How do i sort in descending?

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2008
Posts: 37
Reputation: l_03 has a little shameless behaviour in the past 
Solved Threads: 0
l_03 l_03 is offline Offline
Light Poster

How do i sort in descending?

 
0
  #1
Feb 21st, 2009
  1. package Sorting;
  2.  
  3. import javax.swing.JOptionPane;
  4.  
  5. public class Sorting2 {
  6. public static int s[] = new int[100];
  7.  
  8. public static void main(String[] args) {
  9.  
  10.  
  11. String ask = "";
  12. for (int i = 0; i< 5;i++){
  13. ask = JOptionPane.showInputDialog("Enter: ");
  14. s[i] = Integer.parseInt(ask);
  15.  
  16. System.out.println(s[i]);
  17.  
  18. }
  19.  
  20.  
  21. for (int x = s.length-1; x>= 0; x--) {
  22.  
  23. System.out.println(x);
  24. }
  25. }
  26. }

This is my code ,i want to sort in descending the input that i made,,the problem is it does not output the descending order of the numbers i input. This is my sample input:

Enter: 2 6 8 5 7 9
Output: 9 8 7 6 5 2

But the output is:
Output: 100..............1,

because of the [100] i assigned in int [s], wil somebody help me? i really dont know how to fix it. I hope you can help me. Thank you in advance.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 977
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 145
JamesCherrill JamesCherrill is offline Offline
Posting Shark

Re: How do i sort in descending?

 
0
  #2
Feb 21st, 2009
system.out.println(x); should be system.out.println(s[x]);
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC