Leonardo
Leonardo

Reputation: 11401

Sort based on function

I have a method that given 2 strings he returns a number (between 0 and 100) which represents is how alike they are, being 0 "not similar at all" and 100 "they are the same"

Now the thing is that i have a list of County (string name, GeoRef coordinates, string Mayor) which i would like to sort based on the return of my function...
im looking for something like myList.Sort(f=>MyScoreEvaluator("York",f.Name))

Can anyone tell me how to do so?

Edit1: I dont think that the method "Sort" is quite i want... Sort compare itens inside of the list... i want to compare the itens of the list against a external info and based on that result sort the items

The OrderBy and OrderByDescending are returning the same item order...

Edit2: Heres is the code of the OrderBy I'm using: aux.OrderBy(f => StringComparisonHelper.HowAlike(f.Name, countyNameSearched));

Upvotes: 0

Views: 224

Answers (4)

Jeppe Stig Nielsen
Jeppe Stig Nielsen

Reputation: 62002

It's very easy to use the LINQ OrderBy extension (see others' answers).

If you want to use Sort, it would be:

myList.Sort((x, y) => MyScoreEvaluator("York", x.Name)
    .CompareTo(MyScoreEvaluator("York", y.Name)));

This assumes that myList is a System.Collections.Generic.List<>.

If you want the other sort direction, swap x and y on one side of the lambda arrow =>, of course.

EDIT:

Remember .Sort method on List<> modifies the same instance. The return type of Sort method is void. On the other hand, OrderBy creates a new IEnumerable<> on which you can call .ToList() to get a new list object. The old object is unchanged. You might assign the new object to the variable that held the original list. Other variables that reference the old object won't be affected by that. Example:

myList = myList.OrderBy(f => MyScoreEvaluator("York", f.Name)).ToList();

NEW EDIT:

If performance is an issue, it's not clear which of these two to use. The OrderBy method calls the MyScoreEvaluator only once per item in your original list. The Sort method as presented here, calls MyScoreEvaluator a lot more times, because it doesn't "remember" the result of each MyScoreEvaluator call (the Comparison<> delegate instance is a black box to the Sort algorithm). So if it wants to compare "Fork" and "Kork", it calls MyScoreEvaluator twice. Then afterwards if it wants to compare "Kork" and "Yorc", it does the "Kork" MyScoreEvaluator again. On the other hand, the sort algorithm of List<>.Sort is superior to that of OrderBy.

Upvotes: -1

cuongle
cuongle

Reputation: 75316

You can use OrderBy, and re-assign your list:

list = list.OrderBy(f => MyScoreEvaluator("York", f.Name))

Upvotes: 1

Matthias Meid
Matthias Meid

Reputation: 12523

There is an OrderBy in LINQ:

var sorted = myList.OrderBy(f => MyScoreEvaluator("York", f.Name))

Or to sort descendingly:

var sortedDesc = myList.OrderByDescending(f => MyScoreEvaluator("York", f.Name))

Upvotes: 1

DGibbs
DGibbs

Reputation: 14618

You could just use OrderBy:

list.OrderBy(f => MyScoreEvaluator("York", f.Name))

Or Implement a custom Comparer:

public static int SortByName(County x, County y)
{
    return x.Name.CompareTo(y.Name);
}

Usage:

list.Sort(new Comparison<County>(SortByName))

Upvotes: 1

Related Questions