Custom sorting order for ListBox and/or IComparable

  • Thread starter Thread starter Thomas Lu (Powerex)
  • Start date Start date
T

Thomas Lu (Powerex)

Guest
Hi all,

I am trying to override a ListBox's sorting function to sort items in a non-alphanumeric order.

The ListBox is being populated with a list of development projects' folder names, with the naming convention of CompanyName.ProjectName.DevelopmentPhase, from which users can select environments to install.

For example:

  • MyCompany.Project1.PRD
  • MyCompany.Project1.UAT
  • MyCompany.Project1.TST1
  • MyCompany.Project1.TST2
  • MyCompany.Project1.DEV


I want to change the ListBox so that enabling its Sorted property will sort it in the above order: PRD, UAT, TST, DEV, and then alphanumerically after that.

As I am populating this from a collection of the object class DevelopmentProject, I was wondering if there was (also) a way to implement IComparable<DevelopmentProject>'s CompareTo to sort the collection in this way.


My DevelopmentProject Class is below, with its collection and the ListBox populated by:

string[] projectFolders = Directory.GetDirectories("\\server\developmentprojects");

public List<DevelopmentProject> DevelopmentProjects
{
return new List<DevelopmentProject>(
from subfolder in projectFolders
where ...
select new DevelopmentProject(subfolder));
}

...

DevelopmentProjects.ForEach(x => listBox1.Items.Add(x.Name));




class DevelopmentProject : IComparable<DevelopmentProject>
{
public string Name
{
get {return Path.GetFileName(Location); }
}

private string Location { get; set; };

public string Phase
{
// I am cheating by treating the development phase as a file extension
get { return Path.GetExtension(Location).Substring(1); }
}

public DevelopmentProject(string location)
{
Location = location;
}

public int CompareTo(DevelopmentProject project)
{
//unsure what to do here to sort by Phase in the order or PRD, UAT, TST, DEV, and alphanumerically after
}
}



Any direction as to how I can achieve this for the ListBox and Class's sorting is greatly appreciated.

Thanks,

Thomas

Continue reading...
 
Back
Top