0

I need help with something..

I have an array list containing floating point numbers:

[.43423, .2132, .3242, .....]

I want to create a new array that will sort that list and produce something like

[1, 3, 2, ....]

.43423 becomes 1 because it is the biggest
.3242 becomes 2 because it is the next biggest
etc..

If the decimal points are the same, then they become the same integer value.

How can I go about doing this? Thanks for the help

Sameer
  • 281
  • 1
  • 2
  • 8

4 Answers4

1

If you want to do a descending array here is the code:

Arrays.sort(data, Collections.reverseOrder());

NOTE: you need .reverseOrder to make it descending instead of ascending

The index is now your 0,1,2,3,4 ... etc

If you want to have the same number be the same index you would have to create another array and do it yourself by removing the duplicate element

Jay
  • 2,656
  • 1
  • 16
  • 24
1

I would do this by creating a copy of your original ArrayList then sort it, now I will loop through the original ArrayList and get their index on the sorted ArrayList. This is what I've come up with:

//Initialization
ArrayList<Float> origOrder = new ArrayList<Float>();
origOrder.add(new Float(.43423));
origOrder.add(new Float(.43423));
origOrder.add(new Float(.2132));
origOrder.add(new Float(.3242));

//Copy original ArrayList
ArrayList<Float> sortedOrder = new ArrayList<Float>(origOrder);

//Remove duplicates
HashSet hs = new HashSet();
hs.addAll(sortedOrder);
sortedOrder.clear();
sortedOrder.addAll(hs);

//Sort copy in descending order
Collections.sort(sortedOrder, Collections.reverseOrder());

//ArrayList for result
ArrayList<Integer> result = new ArrayList<Integer>();

//For each float in the original ArrayList
for (Float f : origOrder) {

    //Add their index in the sorted order to the result list, plus 1 to start with 1
    result.add(sortedOrder.indexOf(f) + 1);
}

System.out.println(result);

With the following output:

[1, 1, 3, 2]

Hope this'll help.

lxcky
  • 1,668
  • 2
  • 13
  • 26
0

Best way to do this would be to make a private class Elem {double val, int posInArray}, then create a Comparator on the val field. Sort your array of Elems, and you can't get back the sorted position information as well

ControlAltDel
  • 33,923
  • 10
  • 53
  • 80
0

Here's a class I already had written:

public class HighValueTable
{    
    public final int size;
    private final int[] position;
    private final double[] value;
    private int count = 0;

    public HighValueTable(int numberOfValues)
    {
        this.size = numberOfValues;
        this.position = new int[size];
        this.value = new double[size];  
    }

    public void insert(double valueToInsert, int positionOfNewValue)
    {
        int i;
        i = (count > size) ? count++ : size - 1;
        if (valueToInsert > value[i]) {
            while ((i > 0) && valueToInsert > value[i-1]) {
                value[i] = value[i-1];
                position[i] = position[i-1];
                --i;
            }
            this.position[i] = positionOfNewValue;
            this.value[i] = valueToInsert;
        }
    }

    public double getValue(int index)
    {
        return value[index];
    }

    public int getPosition(int index)
    {
        return position[index];
    }

    public void print()
    {
        System.out.println("[Value] : [Position]");
        for (int pos = 0; pos < size; ++pos) {
            System.out.println(getValue(pos) + " : " + position[pos]);
        }
    }
}

and an implementation:

double[] array = {.43423, .2132, .3242,};
HighValueTable hvt = new HighValueTable(array.length);

for (int i = 0; i < array.length; ++i)
    hvt.insert(array[i], i);

int[] array2 = new int[array.length];

for (int i = 0; i < array.length; ++i)
    array2[i] = hvt.getPosition(i) + 1; // +1 because of zero-based arrays

for (int i : array2)
    System.out.println(i);

Not entirely serious. Just thought I'd share

Fraser
  • 91
  • 8