Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My problem is I need to sort the items in ListBox.

These are my sample items in ListBox.
2021 - January
2021 - February
2022 - January
2022 - February
2021 - April

This is my desired output. I want to sort using Year and then Month.
2021 - January
2021 - February
2022 - January
2022 - February
2021 - April

What I have tried:

I tried
C#
listBox1.Sorted = True;

Yes, it sorted the Year but the output is this.
2021 - April
2021 - February
2021 - January
2022 - February
2022 - January
Posted
Updated 1-Feb-22 3:41am
v2

The problem is that you can't do it like that: you are providing string based data, so the comparison is always going to be string based - and that means that the result of the entire comparison is based on the first different character in the two strings, there is no attempt to "convert it to a date" before comparing.

To display data like that and sort in date order, you have two choices:
1) Set the Listbox values to actual DateTime values, and use the ListControl.FormatString Property (System.Windows.Forms) | Microsoft Docs[^] to display them as "year - month name"
2) use the DataSource property of the Listbox and provide it with two-property values: the DateTime and the string. Set the ValueMember property to the DateTime and the DisplayMember property to the string.
 
Share this answer
 
In this scenario, You need perform some string operations with custom implementation. You can get you listbox items into a collection and perform split operation to seperate year and month values. You can get the listbox items like
string[] names = listBox1.Items.Cast<string>().ToArray();
You can get the month number with the below code.
int month = DateTime.ParseExact(MonthNameStr, "MMMM", CultureInfo.CurrentCulture).Month;
After splitting, populate the collection then apply sort and then assign as datasource to listbox.
 
Share this answer
 

It is not as difficult as it seems if you are familiar with LINQ.


C#
//read the lines into an array
var lines = File.ReadAllLines(@"C:\temp\TestDates.txt");
List<DateTime> dates = new List<DateTime>();
//parse the dates and add then to a list
foreach (var line in lines)
{
    string format = "yyyy - MMMM";
    var date = DateTime.ParseExact(line, format, CultureInfo.InvariantCulture);
    dates.Add(date);
}
//from the dates select a value tuple of type (DateTime date,int index)
var index = dates.Select((d, i) => (d, i))
                 //Sort by DateTime
                 .OrderBy(t => t.d)
                 //Select the index value
                 .Select(t => t.i);
//use the index value to index into the original lines
foreach (var i in index)
{
    //write out the lines in sorted order
    Console.WriteLine(lines[i]);
}

Console.ReadLine();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900