Как сортировать различные типы данных в соответствии с их приоритетом?
I am sorting a list of objects according to a variable stored in them. Lets name that variable "sortCriteria" A sample set of "sortCriteria" values for different objects : 400, 329, 529, "String1", 678, "String2", 588, "String3", "String1", 201, "String2" So, basically there are 4 kind of values I can get in "sortCriteria" : 1. A numeric value 2. String1 3. String2 4. String3 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. Eg. 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.
Что я уже пробовал:
My current Javascript implementation of sorting specific logic looks like this : function mySortRule(a, b) { var value1 = a[1], value2 = b[1]; var value1Priority = getPriorityOf(value1); var value2Priority = getPriorityOf(value2); return value1Priority - value2Priority; } function getPriorityOf(value) { var priority; if(value!="String1" && value!="String2" && value!="String3") { priority = value; } else if(value == "String1") { priority = Number.MAX_VALUE-2; } else if(value == "String2") { priority = Number.MAX_VALUE-1; } else if(value == "String3") { priority = Number.MAX_VALUE; } return priority; } sortCriteriaArray.sort(mySortRule); sortCriteriaArray[i] value is in this format : ["indexOfObject", "sortCriteria"] My 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?