Link or combine 2 arrays into 1 and sort them in java

Is it possible to combine two arrays (one-dimensional), which are strings and int, and sort them? eg:

String name[] = {"Percy", "Daniel, "Layla"}; int marks[] = {90, 87, 91}; Arrays.sort (name); for(int i = 0; i < name.length; i++) System.out.println( (i+1) + ". "+ name[i] + "\t\t" + marks[i]); 

if I would like to sort them, I would like the result to be like this:

 // before sorting 1. Percy 90 2. Daniel 87 3. Layla 91 // after sorting 1. Daniel 90 2. Layla 87 3. Percy 91 // the actual output that i wanted 1. Daniel 87 2. Layla 91 3. Percy 90 

What would you advise me? How to link these 2 arrays and sort them according to their names? Or can I combine them? Is there an easy way that I can understand? Since I read throughout the network using comparators, everything merged, but I donโ€™t understand.

I am new to java. so is there a method for beginners?

+6
source share
9 answers

This is what I usually did when I was a newbie:

  String name[] = {"Percy","Daniel","Layla"}; int marks[] = {90, 87, 91}; String merged[] = new String[name.length]; for (int i=0; i< name.length; i++) { merged[i] = name[i]+"=="+marks[i]; } Arrays.sort(merged); for(int i = 0; i < merged.length; i++) { System.out.println( (i+1) + ". "+ merged[i].split("==")[0] + "\t\t" + merged[i].split("==")[1]); } 
0
source

You are requesting a Java class for beginners. There are many examples on the net about comparators, trees, and everything else in Java. You definitely need to spend time and read everything you see, but many of these examples are crystal clear. If you are trying to learn something, and it does not work for you, do not spend more time on it. Just google again, even if it's the 15th or 20th explanation that finally works for you. This is very common. Just donโ€™t read anything until you understand it.

Of course, you have a class for storing your string, which implements Comparable, as @regulus suggests, except for using a name instead of a label :) Save the character in the class too for future reference or if you want to use it for secondary comparison (after comparing names) . This will give your elements a natural order. When you instantiate each object ...

Paste them into the Java TreeSet instance. Here is an example of its use:

 import java.util.TreeSet; import java.util.Iterator; public class IterateThroughElementsOfTreeSetExample { public static void main(String[] args) { //create object of TreeSet TreeSet tSet = new TreeSet(); //add elements to TreeSet object tSet.add(new Integer("1")); tSet.add(new Integer("2")); tSet.add(new Integer("3")); //get the Iterator Iterator itr = tSet.iterator(); System.out.println("TreeSet contains : "); while(itr.hasNext()) System.out.println(itr.next()); } } 

This would be very fast, because it sorted when you insert the keys.

+2
source

Try something like this:

  String name[] = {"Percy", "Daniel", "Layla"}; int marks[] = {90, 87, 91}; ArrayList<String> arrayList = new ArrayList<String>(); System.out.println("Before Sorting.."); for (int i = 0; i < name.length; i++) { arrayList.add(name[i] + " " + marks[i]); //Before Sorting System.out.println(i + 1 + " " + name[i] + " " + marks[i]); } Collections.sort(arrayList); //After Sorting System.out.println("After Sorting.."); for (int i = 0; i < arrayList.size(); i++) { System.out.println(i + 1 + " " + arrayList.get(i)); } 
+1
source

There is no standard solution. try it

 static void sort(String[] name, int[] marks) { for (int i = 0; i < name.length; i++) { for (int j = i; j > 0 && (name[j - 1]).compareTo(name[j]) > 0; j--) { swap(name, j, j - 1); swap(marks, j, j - 1); } } } private static void swap(String[] x, int a, int b) { String t = x[a]; x[a] = x[b]; x[b] = t; } private static void swap(int[] x, int a, int b) { int t = x[a]; x[a] = x[b]; x[b] = t; } 

This is a modified version of the insertion sorting algorithm from the Arrays.sort array

+1
source

Assuming you have unique names, you can use HashMap to have a pair of names. The card returns its set of keys (name in this case).

 String name[] = {"Percy", "Daniel", "Layla"}; int marks[] = {90, 87, 91}; if (name.length!=marks.length){ System.exit(0); } HashMap<String, Integer> hm = new HashMap<String, Integer>(); for(int i=0;i<name.length;i++){ hm.put(name[i], marks[i]); } ArrayList<String> keys = new ArrayList<String>(hm.keySet()); //for descending order for(int i=keys.size()-1, j=0; i>=0;j++,i--){ System.out.println((j+1)+". "+keys.get(i)+"\t\t"+hm.get(keys.get(i))); } 
+1
source

Your problem can be easily solved with Map . A Map is a class that can be used to store related data pairs, where each pair has a โ€œkeyโ€ and a โ€œvalueโ€. After saving to the map, you can quickly find any value if you have the corresponding key. There is also a way to iterate over or list all the keys on a card.

Here is a simple program showing how to use Map to solve a problem:

 import java.util.*; public class Example { public static void main(String[] args) { String[] name = new String[] {"Percy", "Daniel", "Layla"}; int[] marks = new int[] {90, 87, 91}; // First create a TreeMap to hold the data. A TreeMap is a special // kind of Map which keeps the keys in sorted order for you. // In this TreeMap, the keys will be Strings and the values // will be Integers. TreeMap<String, Integer> map = new TreeMap<String, Integer>(); // Next, link each name in the names array to the corresponding mark // by putting them in the TreeMap. Each name becomes a key // in the map, and each mark is a value. for (int i = 0; i < name.length; i++) { map.put(name[i], marks[i]); } // Now we can iterate over the keys in the map, and for each key // retrieve the corresponding value. The TreeMap guarantees // the keys will be in sorted order. for (String key : map.keySet()) { System.out.println(key + "\t" + map.get(key)); } } } 

Here is the result:

 Daniel 87 Layla 91 Percy 90 
+1
source

Well merging a String array and an integer array does not make sense. If performance is not a priority issue for you, implementing the solution in object-oriented options is much better. I would create a class that contains a name and a character. Thus, there will be an instance of this class for each name and label. Then I would use the Comparable interface, which makes this class sortable.

 class Grade implements Comparable<Grade>{ String name; int mark; public int compareTo(Grade o) { return name.compareTo(o.name); } } 
0
source

There are two ways to do this.

  • If sorting by name only, add the name and labels to the TreeMap value as a key and value that is automatically sorted.
  • If you want to sort both, create a class with these variables and implement a comparable interface.
0
source

Create a New Comparable NameScore

 public class NameScore implements Comparable<NameScore> { private final String name; private final int marks; public NameScore(String name, int marks) { this.name = name; this.marks = marks; } @Override public int compareTo(NameScore other) { // compare by name return this.name.compareTo(other.name); // compare by (ascending) marks //return this.marks - other.marks; } @Override public String toString() { return "" + name + " (" + marks + ")"; } } 

Here's how to use NameScore to solve your question:

 public static void main(String[] args) { String name[] = {"Percy", "Daniel", "Layla"}; int marks[] = {90, 87, 91}; List<NameScore> list = new LinkedList<NameScore>(); for (int i = 0; i < marks.length; i++) { NameScore element = new NameScore(name[i], marks[i]); list.add(element); } System.out.println("BEFORE : "+list); Collections.sort(list); System.out.println(" AFTER : "+list); } 
0
source

Source: https://habr.com/ru/post/956773/


All Articles