I have two arrays First array has large number of words, i have 2nd array smaller to first array and has some words that are occuring in first array. I need to sort 2nd array based on the order of words in first array.
Ex:-
firstarray = ["a","c","b"...];
2ndarray = ["b","c"...];
2ndarrayaftersort = ["c","b"];
First array is a feature vector and 2nd array is weight vector in SVM classification.
Or i need to sort a string of characters…like
“0 35:1 44:1 10:1 45:1 46:1 4:1 47:1
18:1 48:1 49:1 50:1 51:1 52:1 53:1
54:1 55:1 56:1 57:1 58:1 59:1 60:1
61:1 62:1 6:1 63:1 64:1 65:1 66:1 67:1
19:1 68:1 69:1 70:1 71:1”
In the string above we can see that 10:1 is occuring after 44:1 and so on…i need to see that features are sorted or else Libsvm will not classify correctly.
Thanks.
In each case, you simply need a custom way of comparing any two strings. In the first case, assuming your first array is actually quite large, I’d suggest converting the array to a
Dictionary<string, int>so you can efficiently find the right index. Then you’d compare two strings by just finding the corresponding index values and comparing those.In the second case you’d need to split the value into two strings, potentially parse both into integers, and compare those values. You may want to convert your array of strings into a collection of parsed values which are easier to compare, then sort, then convert the values back into strings. That will avoid parsing separately for each comparison.
You can sort an array by passing a custom
IComparer<T>orComparison<T>intoArray.Sort.