Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Monday, November 21, 2016

Sort the contents of an array from the values of a an arraylist

Sort the contents of an array from the values of a an arraylist


i have an array and an arraylist:

String[] league = {"Leeds United","Liverpool","Chelsea","Manchester City","Stoke City"...  List points = new ArrayList();  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

Answer by th3falc0n for Sort the contents of an array from the values of a an arraylist


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)

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

How to sort a Map on the values in Java?

That should be the most adequate solution to your problem.

Answer by MinecraftShamrock for Sort the contents of an array from the values of a an arraylist


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

final Map pointMap = new HashMap<>();  for(int i = 0; i < league.length; i++) {      pointMap.put(league[i], points.get(i));  }    Collections.sort(league, new Comparator() {      public int compare(String a, String b) {          return Integer.compare(pointMap.get(b), pointMap.get(a)); //descending order      }  });  

Answer by Danny Daglas for Sort the contents of an array from the values of a an arraylist


Define this static class somewhere:

private static final class Pair implements Comparable {      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 pairs = new ArrayList<>();  for(int i=0;i

Answer by gerrytan for Sort the contents of an array from the values of a an arraylist


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 points = ...  

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

Arrays.sort(league, new Comparator() {    public int compare(String club1, String club2) {      return -Integer.compare(points.get(club1), points.get(club2));    }  });  

Answer by Makoto for Sort the contents of an array from the values of a an arraylist


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 {          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.

Answer by yurgis for Sort the contents of an array from the values of a an arraylist


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 teamPoints = new HashMap();      for (int i = 0; i < league.length; i++) {          teamPoints.put(league[i], points.get(i));      }      //sort      Arrays.sort(league, new Comparator() {            @Override          public int compare(String o1, String o2) {              //sort in descending order              return teamPoints.get(o2) - teamPoints.get(o1);          }        });  


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.