2

i have an array and an arraylist:

String[] league = {"Leeds United","Liverpool","Chelsea","Manchester City","Stoke City"...
List<Integer> points = new ArrayList<Integer>();
points.add(7);
poinst.add(0);
points.add(5);
points.add(0);
points.add(5);

where :

leeds united = 7

liverpool = 0 etc...

i want to be able to sort the list to be in descending numerical order but the league array should reflect this sort.

is this possible? and how would i do it. thanks

Ivan Chaer
  • 6,980
  • 1
  • 38
  • 48
Aamir Mir
  • 21
  • 1

6 Answers6

2

Define this static class somewhere:

private static final class Pair implements Comparable<Pair> {
    private final String name;
    private final int score;
    private Pair(String name, int score) {
        this.name = name;
        this.score = score;
    }

    @Override
    public int compareTo(Pair p2) {
        return Integer.compare(p2.score, score);
    }

    @Override
    public final String toString() {
        return "["+score+":"+name+"]";
    }
}

and fill a list of Pairs from your score data and sort it:

List<Pair> pairs = new ArrayList<>();
for(int i=0;i<league.length;++i)
    pairs.add(new Pair(league[i], points.get(i)));
Collections.sort(pairs);

System.out.println(pairs);
Danny Daglas
  • 1,501
  • 1
  • 9
  • 9
0

Use the correct storage format to achieve this

First of all you should use an Map

Maps are designed to store a relation from a key to a specific value, in your case from String to int (Map<String, Integer>)

You can then sort the Map like described in this question:

Sort a Map<Key, Value> by values (Java)

That should be the most adequate solution to your problem.

Community
  • 1
  • 1
th3falc0n
  • 1,389
  • 1
  • 12
  • 33
0

This uses Java's Comparator interface and stores the points in a map according to the array:

final Map<String, Integer> pointMap = new HashMap<>();
for(int i = 0; i < league.length; i++) {
    pointMap.put(league[i], points.get(i));
}

Collections.sort(league, new Comparator<String>() {
    public int compare(String a, String b) {
        return Integer.compare(pointMap.get(b), pointMap.get(a)); //descending order
    }
});
MinecraftShamrock
  • 3,504
  • 2
  • 25
  • 44
  • Oh, sorry! I've forgot to get the according integer from the list. Please remove the down-vote or add a comment explaining why the downvote was done and how to improve the answer. – MinecraftShamrock Jan 12 '15 at 21:54
  • how would i use the comparator? sorry i have no experience with it – Aamir Mir Jan 12 '15 at 23:50
0

First you need an associative data structure to hold clubs and its point. A Map sounds like a proper choice.

// Key is club, value is its point
final Map<String, Integer> points = ...

Then you can sort your league array by descending point order using a custom comparator

Arrays.sort(league, new Comparator<String>() {
  public int compare(String club1, String club2) {
    return -Integer.compare(points.get(club1), points.get(club2));
  }
});
gerrytan
  • 40,313
  • 9
  • 84
  • 99
0

A better approach to this problem would be to create an object abstraction of your data which implements Comparable, create instances of those, and sort them in the list with Collections.sort.

    public class FootballClub implements Comparable<FootballClub> {
        private final int rank;
        private String name;

        public FootballClub(final int rank, final String name) {
            this.rank = rank;
            this.name = name;
        }

        public int getRank() {
            return rank;
        }

        public String getName() {
            return name;
        }

        public int compareTo(final FootballClub other) {
            return rank - other.getRank();
        }
    }

Using an array and an ArrayList here makes no sense. Use the data structures that will benefit you the most.

Makoto
  • 104,088
  • 27
  • 192
  • 230
  • footballClubs.put(0, ""Leeds United"); footballClubs.put(0, "Liverpool"); - will update the existing item. does not solve the issue. – vins Jan 12 '15 at 22:05
  • It's covered by other answers, so I'll vary mine a bit. – Makoto Jan 12 '15 at 22:07
0

Here is the self-contained Java (5+) solution with complexity N Log N:

    assert league.length == points.size() : "arrays must have the same size";
    //fill a map
    final Map<String,Integer> teamPoints = new HashMap<String,Integer>();
    for (int i = 0; i < league.length; i++) {
        teamPoints.put(league[i], points.get(i));
    }
    //sort
    Arrays.sort(league, new Comparator<String>() {

        @Override
        public int compare(String o1, String o2) {
            //sort in descending order
            return teamPoints.get(o2) - teamPoints.get(o1);
        }

    });
yurgis
  • 4,017
  • 1
  • 13
  • 22