So, basically there are 4 kind of values I can get in "sortCriteria" :
1. A numeric value
Now I have to sort this data in such a way that the numeric data should be given the most priority, then "String1", then "String2" & then "String3". i.e.
Priority of (Numeric > String1 > String2 > String3)
Note that, in output, all those numeric values should be in sorted order.
Hence, the sorted order of sample data would be -
201, 329, 400, 529, 588, 678, "String1", "String1", "String2", "String2", "String3".
Also, if multiple objects are having same "sortCriteria" values, their order should be retained.
Let say I got 2 objects whose "sortCriteria" value is same
Object 1 : 205,
Object 2 : 205.
Then in sorted order Object1 should come before Object2.
sortCriteriaArray[i] value is in this format :
This solution is kind of working but it's not retaining the objects order. Also, I don't feel that this is a good approach because -
1. Tomorrow, let say we have to fit in some other types of strings. In that case, we will have to change these conditional statements in getPriorityOf() function.
2. Using "Number.MAX_VALUE" to set the priority looks hacky to me.
Can there be any better way to achieve this?
Much of what follows may be wrong because I didn't notice you were writing JS and thought it was Java®. Sorry.
That seems a strange requirement, sorting on different types. I suggest you start by looking at the Java™ Tutorials. There you will find you create a Comparator object. But it isn't plain simple Comparator; it is a generic interface Comparator<T>. And there are very few Ts that will encompass both Strings and numeric types. (Remember your ints can be boxed to Integers.) I don't think var will work because var is reserved for local variables.
My, this isn't easy.
Now, you will find that Strings are easily mutually comparable and so are Integers, but not between types. You can call "String1".compareTo("String2") but not "String1".compareTo(588). I think you will end up casting and calling compareTo() if the two arguments are the same type, otherwise return something > 0 or < 0 depending on whether the first argument is a String.
Then you may end up throwing exceptions if you are passed something of the wrong type. The whole solution will work, but it looks pretty inelegant, because of your strange sorting criteria. Why do you have to use different types? What will happen if you add a third datatype, maybe a double? The whole thing looks to me like a recipe for trouble. Why can't you reduce all the criteria to the same datatype?
As for sorting when two values compare as “equal”: easy. Find out about “stable” sorting algorithms and use one of them.
From what you have shown you actually want (Numeric < String1 < String2 < String3).
Damn! I have just noticed that you are writing JS not Java®. That means most of what I have said will be all wrong Sorry. The bit about == is probably wrong, and maybe I am wrong about var, too. Since I hardly know any JS, who knows what else I have got wrong?
You are making this much harder than it has to be.
First you check to see if the candidates are the same type, if so you simply compare them against one another and return the result of the comparison. If they differ in type, you return the result that makes numbers higher in order than strings. That's pretty much it.