Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
-1
Question by Gilead7 · Mar 01, 2017 at 12:56 AM · c#listsqlitesorting

How do you Sort List by field?

I have a list that is taken from a database. I want to be able to sort it by the first field, which is a date(string) mm/dd/yyyy. When I do, it will sort by the month not by the year. How can I have it sort by the year, then month, then day. Or should this be done on the database level?

Comment
Add comment · Show 8
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Gilead7 · Mar 01, 2017 at 08:58 PM 0
Share

It wouldn't matter if there were more things in the list?

serviceDate(string) placeofService(string) itemCost(string)

then sort?

avatar image UnityCoach Gilead7 · Mar 01, 2017 at 09:34 PM 0
Share

You could store this as a List of custom class, with properties, that you can sort by any given property, using Sort or Linq.

I suggest to store everything in a format as close to what it is (datetime, int, float, string).

avatar image Gilead7 · Mar 02, 2017 at 07:42 PM 0
Share

Having some trouble. I'm using the data type as the class for the list, so it's telling me I can't convert the class to a string. Do I need the unsorted list to be a string? This is the sticky part...

avatar image UnityCoach Gilead7 · Mar 02, 2017 at 07:51 PM 0
Share

Say you have a class or struct Data with a Datetime date property.

You then have a List<Data> dataList. You can sort it with :

 dataList.Sort ((f1, f2) => f1.date.CompareTo (f2.date));

avatar image UnityCoach UnityCoach · Mar 02, 2017 at 07:52 PM 0
Share

And sort it again and again with different properties.

avatar image Gilead7 · Mar 02, 2017 at 07:49 PM 0
Share

$$anonymous$$aybe it would help to have some context:

Here is the class used for the list

 public class OilChange 
 {
     public string ServiceDate{get; set;}
     public string Location{get; set;}
     public string $$anonymous$$ileage{get; set;}
     public string Labor{get; set;}
     public string OilBrand{get; set;}
     public string OilPrice{get; set;}
     public string FilterBrand{get; set;}
     public string FilterPrice{get; set;}
     public string OilFilterPurchaseLocation{get; set;}
 
     public OilChange(string serviceDate, string location, string mileage, string labor, string oilBrand,  string oilPrice , string filterBrand, string filterPrice, 
                      string oilFilterPurchaseLocation)    
     {
         this.ServiceDate=serviceDate;
         this.Location=location;
         this.$$anonymous$$ileage=mileage;
         this.Labor=labor;
         this.OilBrand=oilBrand;
         this.OilPrice=oilPrice;
         this.FilterBrand=filterBrand;
         this.FilterPrice=filterPrice;
         this.OilFilterPurchaseLocation=oilFilterPurchaseLocation;
     }


Here is the function that makes the list public List $$anonymous$$akeOilChangeList() { _connection.Open(); _sqlString = "SELECT * FRO$$anonymous$$ " + SQL_TABLE_OIL_CHANGES + " ORDER BY " + COL_OIL_SERVICE_DATE +" ASC "; _command.CommandText = _sqlString; _command.ExecuteNonQuery(); _reader = _command.ExecuteReader();

                         while (_reader.Read())
                         {
                             OilChangeList.Add(new OilChange(_reader.GetString(0), 
                                                 _reader.GetString(1), 
                                                 _reader.GetString(2), 
                                                 _reader.GetString(3), 
                                                 _reader.GetString(4), 
                                                 _reader.GetString(5), 
                                                 _reader.GetString(6), 
                                                 _reader.GetString(7), 
                                                 _reader.GetString(8)));
                             
                             Debug.Log(_reader.GetString(0) + 
                                                 _reader.GetString(1) +
                                                 _reader.GetString(2) +
                                                _reader.GetString(3) +
                                               _reader.GetString(4) +
                                                 _reader.GetString(5) +
                                                 _reader.GetString(6) +
                                                 _reader.GetString(7) +
                                                _reader.GetString(8));
                         }
 
 
                 _reader.Close();
                 _connection.Close();
                 
         return OilChangeList;
     }




avatar image Bunny83 · Mar 02, 2017 at 08:18 PM 0
Share

$$anonymous$$oved to the help room as it's actually a basic program$$anonymous$$g question and it kind of evolved into a discussion and is constantly bumped.

avatar image UnityCoach Bunny83 · Mar 02, 2017 at 10:39 PM 0
Share

Sure, no problem ;)

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by UnityCoach · Mar 01, 2017 at 01:12 AM

You want to Sort the dates, not the strings. You need to parse the dates.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class DateListParseAndSort : MonoBehaviour
 {
     [SerializeField] private List<string> _dates;
 
     void Start ()
     {
         _dates = new List<string> ();
         _dates.Add ("02/27/2017");
         _dates.Add ("03/15/2015");
         _dates.Add ("11/12/2014");
         _dates.Add ("08/20/2018");
         _dates.Add ("06/12/2019");
 
         Debug.Log ("---------------UNSORTED DATES---------------");
         foreach (string d in _dates)
         {
             Debug.Log (System.DateTime.Parse (d));
         }
 
         _dates.Sort ((f1, f2) => System.DateTime.Parse (f1).CompareTo (System.DateTime.Parse (f2)));
 
         Debug.Log ("---------------SORTED DATES---------------");
         foreach (string d in _dates)
         {
             Debug.Log (System.DateTime.Parse (d));
         }
     }
 }
Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image UnityCoach · Mar 01, 2017 at 01:14 AM 0
Share

You may want to check the date is valid before, or it will throw an error.

avatar image UnityCoach · Mar 01, 2017 at 01:40 AM 0
Share

There, here it is with the check :

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class DateListParseAndSort : $$anonymous$$onoBehaviour
 {
     [SerializeField] private List<string> _dates;
     [SerializeField] private List<string> _datesToSort;
 
     void Start ()
     {
         _dates = new List<string> ();
         _dates.Add ("02/27/2017");
         _dates.Add ("03/15/2015");
         _dates.Add ("13/12/2014");
         _dates.Add ("08/20/2018");
         _dates.Add ("06/12/2019");
 
         Debug.Log ("---------------UNSORTED DATES---------------");
         foreach (string d in _dates)
         {
             System.DateTime date;
             if (System.DateTime.TryParse (d, out date))
             {
                 _datesToSort.Add (d);
                 Debug.Log (date);
             }
         }
 
         _datesToSort.Sort ((f1, f2) => System.DateTime.Parse (f1).CompareTo (System.DateTime.Parse (f2)));
 
         Debug.Log ("---------------SORTED DATES---------------");
         foreach (string d in _datesToSort)
         {
             Debug.Log (System.DateTime.Parse (d));
         }
     }
 }
avatar image UnityCoach · Mar 01, 2017 at 01:41 AM 0
Share

I would recommend storing dates ins$$anonymous$$d of strings, but I didn't know what you plan to do with it, so I kept it as strings all along.

avatar image Gilead7 · Mar 01, 2017 at 04:52 PM 0
Share

I tried it with a date, but it added the time too, which is very hard to take out. That why I wanted strictly strings on this.

avatar image UnityCoach Gilead7 · Mar 01, 2017 at 05:30 PM 0
Share

You can always use .ToString (string format)

 date.ToString ("d");
 date.ToString ("$$anonymous$$$$anonymous$$/dd/yyyy");

avatar image Gilead7 · Mar 01, 2017 at 08:44 PM 0
Share

So you suggest to change them to datetime, then back to string after sort?

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

287 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Getting objects out of a list and then comparing them 0 Answers

Most efficient way to store information in an inspector dropdown menu? 0 Answers

Using a specific function from a generic object. 0 Answers

Setting up a function for sorting specific triggers entered based on a height integer. 0 Answers

check if a list contains the same id as in another list 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges