i made this code to loop number

for(int i=0;i<4;i++){
     double temp[i]=Math.random() * i*10;
     system.out.println("Result" +i+ "=>" +temp);
}

output :

Result 0 => 14.3
Result 1 => 11.4
Result 2 => 10.8
Result 3 => 12.4

now, i want to sort by ascending this output based on value with keep original index, so the output like this :

Result 2 => 10.8 => 1
Result 1 => 11.4 => 2
Result 3 => 12.4 => 3
Result 0 => 14.3 => 4

how to do this?

This is your first post, so you get some extra tolerance, but the code you posted did not produce the output you said it did. If you want our help you must start by being honest with us.

oh, i am sory for my mistake, i promise will post better next time :)
Ok, this is my complete code

public class JavaSort {
    private double[] temp;
    private int i;

    public void evaluation(){
        temp=new double[4];
        for(i=0;i<temp.length;i++)
            temp[i]=i*Math.random();

            sort(temp);
            for(i=0;i<temp.length;i++)
            System.out.println("Result"+i+ "=>"+temp[i]);
    }

    public void sort(double[] temp) {
       for (i = 0; i < temp.length; i++) {
            int min = i; 
            for (int j = i; j < temp.length; j++) {
                if (temp[j] < temp[min])
                min = j;
            }
            double tmp;
            tmp = temp[i];
            temp[i] = temp[min];
            temp[min] = tmp;
        }
  }

    public static void main(String[]args){
        JavaSort k= new JavaSort();
        k.evaluation();
    }
}

in this code, i tried to sort temp[i] by bubble sort, but i don't know how to sort temp[i] with add original index from first loop, as my question above. please help me to solve it.

It's a bit messy, but what you have to do is create a second array containing all the indexes (originally 0, 1, 2, etc).
Then in your bubble sort, every time you swap two values, you also swap the corresponding entries in the array of indexes. So you keep the indexes array in step with the values.

i need your advice again, what the best way to sort like this case, with bubble sort or with java comparator?

You're just sorting numnbers, so a Comparator isn't needed. You need any sort algorithm that you code yourself because you need to keep the index array in synch.

can you help me by give reference that i can learn to keep index array? i still confuse about it, thanks for your response

Thanks, it solved

import java.util.Arrays;
import java.util.Comparator;

public class Sort {

    private Double[] cent_transpos;
    private int i;
    private Integer[] clus;
    private int k=4;

    public void evaluation(){
        cent_transpos=new Double[k];
        clus=new Integer[k];
        Double tempp ;
        for(i=0;i<cent_transpos.length;i++){
            tempp =i*Math.random();
            clus[i]=i;
            cent_transpos[i]=tempp;
        }
      sort(clus,cent_transpos);
    }

    private void sort(final Integer[] clus, Double[] cent_transpos) {
       for( i=0;i<cent_transpos.length;i++)
       System.out.println(+clus[i]+"=>"+cent_transpos[i]);
       Integer[] sortOrder = new Integer[clus.length];

       for(i=0; i<sortOrder.length; i++){
            sortOrder[i] = i;
       }
       printTable("\nNot sorted",clus, cent_transpos, sortOrder);

       Arrays.sort(sortOrder,new Comparator<Integer>() {   
       public int compare(Integer a, Integer b){
                return clus[a]-clus[b];
       }});
       printTable("\nSorted by clus",clus, cent_transpos, sortOrder);
    }

    private void printTable(String caption, Integer[] clus, Double[] cent_transpos, Integer[] sortOrder) {
        System.out.println(caption+
                "\nNo   Clus   Cent_transpos"+
                "\n----------------");

        for(i=0;i<sortOrder.length;i++){
            System.out.printf("%x    %d     %f\n", 
                    i,clus[sortOrder[i]],cent_transpos[sortOrder[i]]);

        }
    }

    public static void main(String[]args){
        Sort k= new Sort();
        k.evaluation();
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.