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?
It wouldn't matter if there were more things in the list?
serviceDate(string) placeofService(string) itemCost(string)
then sort?
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).
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...
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));
And sort it again and again with different properties.
$$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;
}
$$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.
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));
}
}
}
You may want to check the date is valid before, or it will throw an error.
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));
}
}
}
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.
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.
You can always use .ToString (string format)
date.ToString ("d");
date.ToString ("$$anonymous$$$$anonymous$$/dd/yyyy");
So you suggest to change them to datetime, then back to string after sort?
Your answer
Follow this Question
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