• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

sorting arrays in descending order

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Real quick questions... How do I sort an array of int in descending order. Thanks for any help you can give.
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Kevin Knight:
Real quick questions... How do I sort an array of int in descending order. Thanks for any help you can give.



Quick (and hopefully correct) answer: Transfer over to an Integer array, then use Arrays.sort using an appropriate Comparator<Integer> object.

For instance:

[ June 28, 2008: Message edited by: pete stein ]
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch

Pete has already given you the "correct" answer; now I shall give you the answer you want for homework.

Write a class called ArrayUtil or similar and in it implement a public static void swapPair(int[] array, int index1, int index2) method. Pass an array and the numbers of the two elements you want to swap to it. That will swap the two elements. Remember you need a temporary element in the method which will hold the original value while you swap.

Implement a public static void sort(int[] array) method which takes one of the better-known sorting algorithms. Several links to sorting algorithms available: see which of these is most useful: 1 2 3. Lots more available on Google.

Probably best to implement a public static void reverse(int[] array) method which uses the swap method you have already written to reverse the array; that is better than swapping < for > in the sort method.

Use the sort method then the reverse method. And lots of nice marks for you!
 
Kevin Knight
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks guys
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Kevin Knight:
thanks guys

You're welcome
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic