• 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

Arranging an Array in Ascending or 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
I'll be grateful if anybody could tell me how should I arrange the undermentioned array in ascending and descending order:
int ff[]={56,8,75,4,35,90,12,35,123,67,8};
Thanx in advance.
Sajjad Rahimi
Greenhorn
(rather DARK green)
 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Arrays.sort(ff);
See the API (as below):
sort
public static void sort(int[]�a)
Sorts the specified array of ints into ascending numerical order.
The sorting algorithm is a tuned quicksort, adapted from Jon
L. Bentley and M. Douglas McIlroy's "Engineering a Sort Function",
Software-Practice and Experience, Vol. 23(11) P. 1249-1265 (November
1993). This algorithm offers n*log(n) performance on many data sets
that cause other quicksorts to degrade to quadratic performance.
Parameters:
a - the array to be sorted.
 
Sajjad Rahimi
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Mr. Hoburn:
Thanx for your help. But it's a bit difficult for me to grasp, since, you see, I'm Java's DARKgreenhorn (very new to Java). I'll appreciate having proper coding for this purpose.
Regards,
Sajjad Rahimi

Originally posted by James Hobson:
Arrays.sort(ff);
See the API (as below):
sort
public static void sort(int[]�a)
Sorts the specified array of ints into ascending numerical order.
The sorting algorithm is a tuned quicksort, adapted from Jon
L. Bentley and M. Douglas McIlroy's "Engineering a Sort Function",
Software-Practice and Experience, Vol. 23(11) P. 1249-1265 (November
1993). This algorithm offers n*log(n) performance on many data sets
that cause other quicksorts to degrade to quadratic performance.
Parameters:
a - the array to be sorted.


 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
int ff[]={56,8,75,4,35,90,12,35,123,67,8};
Arrays.sort(ff); // sorts in ascending numerical order and puts back in ff
Why would you sort it in descending order? Just read the array from end to front.
------------------
Cindy Glass
Sun Certified Programmer
for the Java� 2 Platform
Co-author of Java 2 Certification Passport
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
don't forget to put
import java.util.*;
at the top of your file, or the compiler won't have any idea what Arrays.sort() is. i found out the hard way >;P
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic