3

Having a List<Office> where Office is a class, I have to sort its entries by country (where country is a property of class Office).

Some offices have no country set and therefore will be displayed at the top of the list. In this case I have to put them at the bottom of the list since considered "less relevant".

switch (sortOptions.SortField)

...  

case OfficeSortField.Country:

var noCountryList = officesList.Where(a => string.IsNullOrEmpty(a.CountryText)).ToList();
officesList.RemoveAll(a => string.IsNullOrEmpty(a.CountryText));

officesList= sortOptions.SortOrder == SortOrder.Ascending
                                      ? officesList.OrderBy(o => o.CountryText).ToList()
                                      : officesList.OrderByDescending(o => o.CountryText).ToList();

officesList.AddRange(noCountryAssoList);
break;

Under perfomance perspective, is there a better way to proceed?

4

7 回答 7

2

The performance differences you would notice for these sort of things will be so minor its not worth worrying about. If you are not already doing so I would use Linq to carry out the logic to just sort by whatever you want, so you don't have to do any removing/inserting.

Then if you want to improve performance further look at using Plinq to spread the logic over multiple cores.

于 2012-09-21T07:42:58.637 回答
2

The best way is to sort using a custom function.

You can do it as a delegate:

list.Sort((first, second) =>
      {
         // Your code to compare first and second items
         // return 0 if equal, -1 or +1 for other cases
      });

You could do all your process in one pass, you don't need to extract offices without countries.

于 2012-09-21T07:43:13.473 回答
2

Sure there is. Try this one:

List<Office> list = new List<Office>(...);

list.Sort((x, y) => x.Country == null ? (y.Country == null ? 0 : -1) :
    (y.Country == null ? 1 : Comparer<Office>.Default.Compare(x, y))

Or, you should better implement the comparer (if you are going to reuse the sorting thing).

class OfficeComparer : IComparer<Office>
{
    public int Compare(Office a, Office b)
    {
        return a.Country == null ? (b.Country == null ? 0 : -1) :
            (b.Country == null ? 1 : Comparer<Office>.Default.Compare(a, b))
    }
}

Then you can use it:

List<Office> list = new List<Office>(...);
list.Sort(new OfficeComparer());
于 2012-09-21T07:43:35.207 回答
2

The best way for this is to implement a comparer:

class OfficeComparer:IComparer<Office>
{
        int IComparer.Compare(Office a, Office b)
        {
               if ( a.Office.Country != null && b.Office.Country != null) 
                       return a.Office.Country.CompareTo(b.Office.Country);
               if ( a.Office.Country == null && b.Office.Country != null) return -1;
               if ( a.Office.Country != null && b.Office.Country == null) return 1;  
               return 0; // if both have no country, return equal or whatever other criteria comparaison

        }
}

in your comparer, you just give a low priority to office without a country, and then just call the sort method:

List<Office> lst = FillList();

lst.sort(new OfficeComparer());
于 2012-09-21T07:44:24.347 回答
0

You can use IComparable or ICompare interface and define which object of two object is "greater" than second.

You can find more in this knowledge base article on How to use the IComparable and IComparer interfaces in Visual C#

于 2012-09-21T07:43:42.023 回答
0

This is most easiest and performance effective, I retrieve all physics book, in that I want to push the category "other" to be listed in end of the list.

List<Books> controlGroupDetails = controlDetails.Where(s => s.Title == "Physics").ToList();
var otherPhysics = controlDetails.Where(s => s.Title == "Physics" && s.Name == "Other").SingleOrDefault();
controlGroupDetails.Remove(otherPhysics);
controlGroupDetails.Insert(controlGroupDetails.Count(), otherPhysics);
于 2017-02-06T08:07:07.677 回答
-1

In Java, you can implement Comparable<T> interface and then sort the list by invoking java.util.Collections.sort(List<T> list).

See details:

public class Office implements Comparable<Office> {
  private String country;
  public int compareTo(Office off) {
    if (this.country == null)
      return -1;
    else if (off.country == null)
      return 1;
    else
      return this.country.compareTo(off.country);
  }
}

Sort the list:

java.util.Collections.sort(yourOfficeList);
于 2012-09-21T07:40:45.723 回答