So, I'm pretty new to C# and I'm trying to order an Array using a custom Comparer.
I created a class:
class MySorter : IComparer
{
public int Compare(object x, object y)
{
var chars = "jngmclqskrzfvbwpxdht";
if (chars.IndexOf((char)x) < chars.IndexOf((char)y))
return -1;
return chars.IndexOf((char)x) > chars.IndexOf((char)y) ? 1 : 0;
}
}
And I have a Array full of words. How exacly I can use this Compare to sort?
The basic idea is to create a std::vector<std::pair<int,int>> and then simply sort that via std::sort . The rest of the code is about copying the values and ranks into that vector and copying the values out of it after sorting.
To sort an array in Java in descending order, you have to use the reverseOrder() method from the Collections class. The reverseOrder() method does not parse the array. Instead, it will merely reverse the natural ordering of the array.
I think that what you need is this. Declare a method for sorting as you already did.
public static int CompareStrings(string s1, string s2)
{
// TODO: your code here
}
... and specify what function you need to use.
string[] myStrings = { ... };
Array.Sort(myStrings, CompareStrings);
If you use generic classes, you can achieve this as well doing as it follows:
List<string> myStrings = ...;
myStrings.Sort(CompareStrings);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With