-3

I have two classes

1 class Runner : instance variables = name , time.

2 class MarathonAdmin

I created list -runners- which hold Runner class objects.object's time are random numbers , generated by another method in class MarathonAdmin. now i want to sort list runners by TIME property of Runner objects in such a way that smallest time taken by runner come first and than so on . I tried to get two lists that takes time of runners and reversed one list so that i get different time to compare with each other , but that didnt work , so i need help . I have to compare time with each other . my code written so far is

public void sortRunnerList(){ for(Runner nameRunner : runners){

int time1 = nameRunner.getTime();

List<Integer> time1list = new ArrayList<>();
 time1list.add(time1);
Collections.reverse(time1list);
System.out.println( "time for obj1"   + time1list);

}

for(Runner nameRunner : runners){

int time2 = nameRunner.getTime();

List time2list = new ArrayList<>(); time2list.add(time2);

//Collections.shuffle(time2list);

System.out.println( "time for obj2"  + time2list);

} }

and for compareTo method

 @Override
  public int  compareTo(Runner anotherRunner)
   {

       return this.getTime()-(anotherRunner.getTime());

   }

2 Answers2

0

you need to tmplement Comparable and override compareTo() method to compare your objects on the basis of time property

then you simple can use Java's Collections to get those sorted

dev2d
  • 4,245
  • 3
  • 31
  • 54
  • I wrote comparable and override compareTo also .letme show you sample i have to sort time taken by each runner , in such a way that the smallest time come first and so on . here is code – user3602257 May 04 '14 at 20:22
  • then paste your code and ask for specific problem – dev2d May 04 '14 at 20:22
  • for(Runner nameRunner : runners){ int time1 = nameRunner.getTime(); List time1list = new ArrayList<>(); time1list.add(time1); Collections.reverse(time1list); System.out.println( "time for obj1" + time1list); } for(Runner nameRunner : runners){ int time2 = nameRunner.getTime(); List time2list = new ArrayList<>(); time2list.add(time2); //Collections.shuffle(time2list); System.out.println( "time for obj2" + time2list); } – user3602257 May 04 '14 at 20:25
  • i am trying to compare time with each other , thatsy trying to store the got time in two lists ans than reverse or shuffle one of the list so that i could compare them – user3602257 May 04 '14 at 20:27
  • you need to implement one small method only and then do as it is shown by Moud.fkr in below ans – dev2d May 04 '14 at 20:33
0

Try This :


//This is a simple implementation of your Runner Class
public class Runner implements Comparable  {
    String name;
    double time; //for exemple

    @Override
    public int compareTo(Runner o) {
        Double a = this.time;
        Double b = o.time;

        return a.compareTo(b);
    }
}

//Class Main
public class Main {
    public static void main(String[] args) {

        List list = new ArrayList();
        list.add(new Runner());
        list.add(new Runner());
        list.add(new Runner());

                //Print the list before sort

        Collections.sort(list);

              //print the list after sort and see the result
    }
}

Mouad EL Fakir
  • 3,609
  • 2
  • 23
  • 37
  • this would be applicable if time was there . but the case is that TIME assigned to Runners is by method randomNumberGenerator() which generated random numbers and than we iterate over runners and assign them random numbers as their time – user3602257 May 04 '14 at 20:37
  • should i write complete code I have written so far? its all working well . except the case of sorting , might be from whole code you get what I am trying to ask – user3602257 May 04 '14 at 20:37
  • i can paste my complete code here , I need help to sort our this list . – user3602257 May 04 '14 at 20:39
  • i'm giving a general solution you can adjusted as you like , but it's Ok i'm going to adjust the code for you to mutch what you need ;) – Mouad EL Fakir May 04 '14 at 20:40
  • thank you so much than i am pasting my code here . i am stuck with this method for couple of days ,ur help would be much much appreciated :) – user3602257 May 04 '14 at 20:42
  • code is very long cannot be pasted in comment . should i add it to my original question ? I am new to stackoverflow . so much apologies if i ask any stupid question like code pasting and all that stuff .. – user3602257 May 04 '14 at 20:47