This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Five Lines of Code and have Christian Clausen on-line!
See this thread for details.
Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
  • Piet Souris
  • Frits Walraven
  • Carey Brown

How to sort different types of data according to their priority?

Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
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 :

sortCriteriaArray[i] value is in this format :
["indexOfObject", "sortCriteria"]

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?
Posts: 69789
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Why are you using the == operator on reference types?
Campbell Ritchie
Posts: 69789
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tony Singh wrote:. . .  (Numeric > String1 > String2 > String3). . . .

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?
Posts: 67418
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic