user3147409
user3147409

Reputation: 1

Sorting arrays with the same order

I'm making a program to make a list of people, with 3 different marks of time (in double) To do it, I made 4 arrays, One String, to save people names, and 3 Doubles to save the 3 marks on the years 2010, 2011, and 2012. In the menu, I have to implement an option to sort the list on 2012s mark, in descending order.

Like this

m12[0] = 12.1
m12[1] = 34.1
m12[2] = 23.1
m12[3] = 23.5

into:

m12[1] = 34.1
m12[3] = 23.5
m12[2] = 21.1
m12[0] = 12.1

I did it with a basic algorithm, but now I want to know if it's possible to get the actual order of the arrays, ([1],[3],[2],[0]) and apply it to the other arrays I have to print it as a list based on the 2012 mark in descending order.

Thats the code I have to make the normal order list:

            if(option==2){
            System.out.println("# , Name, 2010, 2011, 2012");
            for(i=0;i<dorsal.length-1;i++){
                if(dorsal[i]!=0){
                    System.out.println(dorsal[i]+"- "+nom[i]+", "+m10[i]+", "+m11[i]+", "+m12[i] );
                }
            }
            System.out.println("Press ENTER to return");
            intro.nextLine();
        }

Sorry if I didnt explained it very good, I started programming 3 months ago and I'm so newbie.

//EDIT I'll paste here the head of the exercise:

Thats exactly the programs needs to do. I'm stucked at point 3.

The objective is to develop a program to manage a list of members of in a competition of long jump. The number of places available is 15. Their data will be introduced in the same order in which the athletes enroll. Design a program that shows the following options:
1 – Register a participant
2 – List all the participant’s data
3 – List all the participant’s data by mark
4 – Quit
If 1 is selected, data of one of the participants will be introduced:
Name, best mark in 2012, best mark in 2011 and best mark in 2010.
If 2 is selected, we have to list all participant’s data ordered by dorsal number (the order they’ve enrolled)
If 3 is selected, we have to list all participant’s data ordered by 2012 mark, from greater to smaller.
After processing each option, the main menu must be shown again, till the option 4 is selected, quitting the program.

Thanks.

Upvotes: 0

Views: 101

Answers (1)

Jim Garrison
Jim Garrison

Reputation: 86754

Define a class to contain the data for each person, such as:

public class Person
{
    private String name;
    Private Map<Integer,Double> marks = new HashMap<Integer,Double>();
    public Person(String name) { this.name = name; }
    public void setMark(int year, double mark) {
        this.marks.put(year,mark); 
    }
    public void getMark(int year) {
        // return zero if there's no mark for the requested year
        return this.marks.containsKey(year) ? this.marks.get(year) : 0;
    }
}

Then write a Comparator<Person>

public PersonComparatorOnMarkDescending implements Comparator<Person>
{
    private int yearToCompare;
    public PersonComparator(int yearToCompare) {
        this.yearToCompare = yearToCompare;
    }
    public compare(Person p1, Person p2)
    {
        Integer p1Mark = p1.getMark(yearToCompare);
        Integer p2Mark = p2.getMark(yearToCompare);
        return p2.compareTo(p1);
    }
}

You can then define a List<Person> or a Person[] array and use the sorting methods available in java.util. Instantiate the comparator with, for instance:

Comparator<Person> comp = new PersonComparatorOnMarkDescending(2012);

This approach lets you sort the collection on any year's marks.

Upvotes: 2

Related Questions