Tom
Tom

Reputation: 11

how to get two arrays to be sorted the exact same way, so that

...when scores in one array are re-arranged into descending order the other array has the correct name to the score, for instance, when cliff scores 1, Dave scores 2, Eric scores 3, Bob scores 4 and Andy scores 5... the score table SHOULD look like this (not formated as it will be in the output just to show what I need to happen to the data.) : Andy 5, Bob 4, Eric 3, Dave 2, Cliff 1. Or atleast this is how the names need to be arranged, so that they remain in the same order/paired up with their score as the scores come out. how can you do this? Here's the full program so far, the section I'm focusing on just now is the two method s: "Sort". I imagine it's just my understanding of how the swap or sort function works that is at fault. I am only really concerned with the sorting and printing out of the array the rest is there only to show a bit more of what the program should do.

all help would be much appreciated :)

import java.util.Scanner;
public class InThirdPlace {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String candidates[] = {"cliff", "Dave", "Eric", "Bob", "Andy"};
        int votes [] = new int [5];
        int vote = 0;
        System.out.println("please enter candidate no. (0-4) you wish to vote for");
        vote = input.nextInt();
        while (vote != -1){
            if (vote >= 0 && vote <= votes.length - 1){
                votes[vote] ++ ;}
        else{
            System.out.println("please enter a valid value.");}   
            System.out.println("Enter a vote (0 -4)");
             vote = input.nextInt();}
        sort(votes, candidates);
        printArray(candidates, votes);
        System.out.println("3rd "+ candidates[2]);
            System.out.println("in 2nd "+ candidates [1]);
            System.out.print("1st " + candidates [0]);
}

public static void sort(int votes[], String names[]){

    for (int i = votes.length; i>=2; i--){

        int largestIndex = findPositionOfBiggest(votes,i);

        swap(votes, i-1, largestIndex);
        swapNames(names, i-1, largestIndex);
    }
}


public static int findPositionOfBiggest(int votes[], int numItems){
    int indexLargest=0;//Set the largest index to 0

    for(int loop=1;loop<numItems;loop++){

        if (votes[loop]>votes[indexLargest]){

            indexLargest = loop;
        }
    }
    return indexLargest;
}

public static void swap(int votes[], int pos1, int pos2 ){

    int temp = votes[pos1];
    votes[pos1] = votes[pos2];
    votes[pos2] = temp;
}

public static void swapNames(String names[], int pos1, int pos2 ){

    String temp = names[pos1];
    names[pos1] = names[pos2];
    names[pos2] = temp;
}
public static void printArray(String names[], int votes[]){
    for (int i=0; i < votes.length; i++){
        System.out.println(names[i] + votes[i] + " ");
    }
    System.out.println();
}
}

Upvotes: 1

Views: 1048

Answers (2)

Peter Lawrey
Peter Lawrey

Reputation: 533472

Normally you would use an Object like the following as Java is an Object Orientated language.

class Votes {
    final List<NameVote> nameVotes = new ArrayList<NameVote>();

    public void add(String name, int votes) {
        nameVotes.add(new NameVote(name, votes));
    }

    public void sort() {
        Collections.sort(nameVotes, new Comparator<NameVote>() {
            @Override
            public int compare(NameVote o1, NameVote o2) {
                return o1.votes - o2.votes;
            }
        });
    }

    public String toString() {
        StringBuilder sb = new StringBuilder();
        for (NameVote nameVote : nameVotes) {
             sb.append(nameVote).append('\n');
        }
        return sb.toString();
    }
}

class NameVote {
    final String name;
    final int votes;

    NameVote(String name, int votes) {
        this.name = name;
        this.votes = votes;
    }

    @Override
    public String toString() {
        return "name='" + name + '\'' +
                ", votes=" + votes;
    }
}

Upvotes: 0

John Kane
John Kane

Reputation: 4443

Why not make your array an array of an Object that you create? Then you can tie the name and score together in one place and when you sort the array the name is already associated with it.

Upvotes: 3

Related Questions