Results 1 to 11 of 11
Like Tree1Likes
  • 1 Post By JosAH

Thread: Sorting by 2 fields

  1. #1
    Join Date
    Aug 2013
    Posts
    108
    Rep Power
    0

    Default Sorting by 2 fields

    In an interview they said there is an Employee class with two fields say name and salary and the interviewer was asking me if I could sort based on both fields using a comparator interface. I clarified 3 times if he wanted to sort based on one field and then if there are same values in the first field then should I sort based on the 2nd field. He said no. He said you have to sort on both fields simultaneously.

    Can somebody explain.

    Thanks in advance.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default Re: Sorting by 2 fields

    There is no need, indeed, to sort twice as you suggested. In the implementation of the Comparable<T> interface or the implementation of the Comparator<T> interface do this:

    Java Code:
    int result= this.name.compareTo(that.name);
    if (result != 0) return result;
    if (this.salary < that.salary) return -1;
    if (this.salary > that.salary) return 1;
    return 0;
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Join Date
    Aug 2013
    Posts
    108
    Rep Power
    0

    Default Re: Sorting by 2 fields

    The code you mentioned was the same thing I told the interviewer but he said I dont want to sort based on name and then check for salary but want to check both simultaneously. God knows what the interview meant.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default Re: Sorting by 2 fields

    That interviewer must've been a real sicko; maybe he wanted a godelization of the two keys; build a fixed sized String representation of the salay (e.g. prepend enough zeros) and catenate the name and that String representation with, say, a dollar sign in between, e.g. "Jones$0000001000.00" and compare those two godelizations ... it's sick ... ;-)

    kind regards,

    Jos
    gimbal2 likes this.
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: Sorting by 2 fields

    Jebus, I wouldn't have thought of that in a million years. Nice one Jos.

    Evil rant mode on:

    Its STILL only a party trick and not really a useful thing to ask in a job interview. I'd rather test who people are and how much experience they have in stead of being a useless lazy bastard and stick to trivia and riddles. I have over a decade of working experience and you'd lose out on hiring me, that's for sure. Not necessarily because I'd fail the test (but probably yes), but because I'd walk out of the interview and move on to a company where the first impression given is not saddening.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: Sorting by 2 fields

    "What do public, private and protected mean?"

    ...did you read my CV?
    I've been doing Java for 15 years.

    That one always makes me chuckle...
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default Re: Sorting by 2 fields

    Quote Originally Posted by gimbal2 View Post
    Jebus, I wouldn't have thought of that in a million years. Nice one Jos.
    Godelization is an old trick and only interesting from a theoretical point of view; and mostly with numbers; and only if you have infinite sets of them, but when you think too much about inifinity your mind goes gaga (see Godel, Cantor etc.) It is also very inefficient from a computational point of view; for no particular reason I remember how my old professor used to count: zero, one, many, many more ... what I find fascinating is that most of the theoretical footwork was already done long before the real electronic computers saw the light ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: Sorting by 2 fields

    That's true for most everything in the computing world - mathematicians made it up, computer engineers implemented it.

    An interesting example is the way worlds are stored in the somewhat well known game Doom - using a Binary Space Partition tree. Its what made it possible to define such a world on a "lowly" 386 even. John Carmack applied the technique in 1993 and revolutionized 3D game worlds, but the data structure and algorithm were made up by a mathematician in the 60's, outside of the scope of computer simulations.
    Last edited by gimbal2; 08-01-2013 at 11:09 AM. Reason: horrible, horrible grammar mistake
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default Re: Sorting by 2 fields

    Quote Originally Posted by gimbal2 View Post
    but the data structure and algorithm was made up by a mathematician in the 60's, outside of the scope of computer simulations.
    See? That shows it again: all mathematicians belong inside a loony bin ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: Sorting by 2 fields

    Quote Originally Posted by Tolls View Post
    "What do public, private and protected mean?"

    ...did you read my CV?
    I've been doing Java for 15 years.

    That one always makes me chuckle...
    Because you can use it, or because someone uses it on you?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: Sorting by 2 fields

    Quote Originally Posted by JosAH View Post
    ...I remember how my old professor used to count: zero, one, many, many more ...
    Was he a descendant of a lost South American tribe?
    That's how many hunter gatherer tribes seem to count..."1,2,some,lots,many".
    After all, a couple of capybara'll sort out the tribe for a day or two, who needs more?

    Quote Originally Posted by gimbal2 View Post
    Because you can use it, or because someone uses it on you?
    I've had it used on me (recently in fact).
    I don't interview people...well, I interview the interviewer, but that's not the same thing.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Sorting a Vector by Fields of Objects Stored in it
    By Willriker in forum New To Java
    Replies: 10
    Last Post: 06-15-2013, 04:18 PM
  2. Static fields
    By riverhare in forum New To Java
    Replies: 2
    Last Post: 05-30-2013, 07:57 AM
  3. Replies: 8
    Last Post: 08-13-2011, 06:02 PM
  4. re-indexing particular fields
    By caballo7 in forum Lucene
    Replies: 0
    Last Post: 06-08-2010, 06:46 AM
  5. instance fields?
    By search4survival in forum New To Java
    Replies: 3
    Last Post: 04-03-2010, 05:10 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •