• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Sorting order

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Can someone explain me how does sorting work here.When -1 is returned from compareTo then the elements are arranged in insertion order but when a 1 is returned then the elements are arranged in reverse of the insertion order.How does it actually work.I get confused sometimes.Is there any clue on the basis of which we can decide the sorting order.
Thanks in advance.Below is the code for reference.


 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please, give the source of this question (according to JavaRanch policy.)

Thanks.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well compareTo of comparable interface returns int as you know in that -1 is if the passed object is greater than the invoking object meaningfully(this one depends on your parameter), in the above specified one i think you want to sort based on age .Wrapper classes and String has already implemented Comparable based on natural ordering so if you change the age into Integer and return type of getAge as Integer then person[0].getAge().compareTo(person[1].getAge) will give you -1 as 20 is < 30 so for descending order just interchange the above ,

hope that you understood
 
C Kushtawar
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok this question is from javaBeats forum.

Hi vadlamani ,

I think here the only sorting is bing done on the insertion oder not on age.
Can you explain this?
Thanks.
 
raghuram vadlamani
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hai Kushtwar,

Arrays.sort uses Modified MergeSort algorithm for sorting and API says that it does it in natural ordering of the element and ordering means it has to make some sense rather than random arrangement and the API specifys that it arranges it in ascending order so based on this best way to understand it is using bubble sort as it is the simplest sorting algorithm useful for small list

{person[0],person[1],person[2]} well with natural ordering i.e least will be first so

person[0].compareTo(person[1]) which returns 1 think like >0,0,<0 rather than 1 , 0, -1 as you will get the same output if you return 1,11,11121.... any postive number greater than 0(ofcourse has to be compatible with int )

so person[0] is greater than person[1] now according to bubble sort swap them

then you will have
{person[1],person[0],person[2]}

now person[0].compareTo(person[2]) which is also positive so person[0] is greatest of three so now list will be {person[1],person[2],person[0]}

now person[1].compareTo(person[2]) and person[1] is greater than person[2] so the least is person[2] and obviously the list will be
{person[2],person[1],person[0]}

now using the same least will be first principle if negative number is returned from compareTo then person[0] will be the least then successive comparasion will be same and thus no effect it is the same with 0

I hope you understood
 
C Kushtawar
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vandlamani,


Can you make me understand the following code.Could you crosscheck my below understanding.








My understanding:

Here s1=Good
s2=Bad
s2.charAt(1)-s1.charAt(1) will return negative number then s2<s1 then the order should be Bad,Good which means words[0] Bad
but that is not the correct answer. I know I am wrong but where ;please help me clearing this concept.

Regards,
Chandan

Hi team,request to provide some response to it.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic