0

I am trying to sort candidate names alphabetically while sorting votes gained by candidate,I took two arrays one for names and another for votes,as i sort by votes name array need to sort here i can't make it sort please help here is my code:

package com.sarga.Swinglearn;
import java.util.Scanner;

public class Project3 {

public static void main(String[] args) 
{

    int i=0,j=0;
    Scanner s=new Scanner(System.in);
    System.out.println("Enter number of candidates");
    int candcount = Integer.parseInt(s.nextLine());
    System.out.println("Enter name of the candiadates");
    String names[]=new String[candcount];//create an array
    for( i=0;i<names.length;i++)
    {
        names[i]=s.nextLine();
    }
    System.out.println("candidates are: ");
    for(i=0;i<candcount;i++)
        System.out.println(names[i]);
    for(i=0;i<candcount;i++)
    {
        for(j=i;j<candcount;j++)
        {
            if(names[i].compareTo(names[j])>0)
            {
                String temp=names[i];
                names[i]=names[j];
                names[j]=temp;  
            }
        }   
    }
    /*To sort names alphabetically*/
    System.out.println("alphabetical order of candidates");
    for(i=0;i<candcount;i++)
    {
        System.out.println(names[i]);
    }
    System.out.println("Enter number of votes of each candidate");
     int votes[]=new int[candcount];
    for(i=0;i<candcount;i++)
    {
         votes[i]=s.nextInt();
        System.out.println(names[i]+":"+votes[i]);
    }
    //sort names based on their votes
    System.out.println("List of candidates according to their votes");
    //int max= votes[1];
    int temp=0;
    for(i=0;i<candcount-1;i++)
    {
        for(j=i;j<candcount;j++)
        {
            if(votes[i]<votes[j])
            {
             temp=votes[i];
             votes[i]=votes[j];
             votes[j]=temp;
            }
        }
    }
    for(i=0;i<candcount;i++)
    System.out.println(names[i]+":"+votes[i]);
    s.close();
}

}
2
  • 3
    I suggest you create a class that holds name and vote-count. Then you can make an array of that type and when sorting by vote-count, you'll get the correct order of the names. Btw: There is Sort in the Arrays-Class Commented Dec 5, 2016 at 11:55
  • 3
    Create a new Object to encapsulate name and votes and store in ArrayList and use Collections.sort with custom comparator Commented Dec 5, 2016 at 11:55

2 Answers 2

1

Create a Candidate class:

public class Candidate implements Comparable<Candidate> {
    private String name;
    private int votes;

    public Candidate(String name, int votes) {
        this.name = Objects.requireNotNull(name);
        this.votes = votes;
    }

    // Getters and setters

    @Override
    public int compareTo(Candidate that) {
        int c = this.name.compareTo(that.name);
        if(c != 0) return c;
        return this.votes - that.votes;
    }
}

Next create a list of those candidates and sort them:

List<Candidate> clist = new ArrayList<>();
// Add some Candidates to clist
Collections.sort(clist);
Sign up to request clarification or add additional context in comments.

Comments

0

you shoul use object oriented paradigm ; create a Candidate class which implements the Comparable interface :

public class Candidate
    implements Comparable<Candidate>
{
    public String name; /* should use getter and setter */
    public int votes; /* idem */

    public int compareTo(Candidate other)
    {
         /* implements the comparison, see Comparable doc */
    }
}

Then sort a Candidate array in your main :

Candidate[] candidates = new Candidate[candcount];
/* populates the array */
Arrays.sort(candidates);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.