Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

how to Sort?

  Asked By: Wayne    Date: May 28    Category: Java    Views: 699
  

I have an array of double values that I need it to be
sorted, from high to low, is there any method in java
that can do this, I tried Array.sort(arr) , but it
changes my values in the array to 0.
Do we have a quick sort algorithm written in java,

Share: 

 

4 Answers Found

 
Answer #1    Answered By: Bama Cohen     Answered On: May 28

Take a look at java.util.Arrays sort  method. It
DOES work. I tried the following code, just for
testing:

import java.util.Arrays;

public class Test
{
public static void main(String[] args)
{
double[] arr = {3.5d, 4.5d, -1.7d, 4.4d,
2.000001d, 2.0001d, 1d };

for (int i=0; i<arr.length; i++)
{
System.out.println("arr[" + i + "]: " +
arr[i]);
}

Arrays.sort(arr);

for (int i=0; i<arr.length; i++)
{
System.out.println("arr[" + i + "]: " +
arr[i]);
}
}
}


What are the values  that you are placing into the
array? How are you using the values? When you get 0s,
how are you figuring out that they are, indeed, 0?
Could it be a printing/formatting issue?

 
Answer #2    Answered By: Hadil Khan     Answered On: May 28

I assume you meant Arrays.sort(arr), not Array.sort(arr).

That should sort  the array  OK. If it didn't work for you, post your code and
we'll look at it.

However, you indicate "high to low", so you're talking a descending sort. For
that, I think you need to go to Collections.sort and use a comparator. See
java.sun.com/.../Collections.html for
information.

 
Answer #3    Answered By: Dale Jones     Answered On: May 28

Better go for double  warpper class to store the double values  and use
SortedSet from
Collection for ordering ur values either ascending or decending.

 
Answer #4    Answered By: Nicolas Costa     Answered On: May 28

I have an array  of double  values that I need it to be
sorted, from high  to low, is there any method  in java
that can do this, I tried Array.sort(arr) , but it
changes my values  in the array to 0.
Do we have a quick sort  algorithm written  in java.

 
Didn't find what you were looking for? Find more on how to Sort? Or get search suggestion and latest updates.




Tagged: