Reputation: 33
I have data such as
student one 12 22 78
student two 21 24 83
student three 10 12 67
in the data file their are approx 35 students
Is their an a way to store this data in an array and then sort the data in the values in the last coloumn.
i have this and was thinking i need to use comparator function but will this allow me to sort because the array is for string but includes numbers
String[][] aryStudents = new String[4][4];
aryStudents[0][1] = "Student1";
aryStudents[0][2] = "12";
aryStudents[0][3] = "22";
aryStudents[0][4] = "73";
aryStudents[1][1] = "Student2";
aryStudents[2][2] = "12";
aryStudents[3][3] = "22";
aryStudents[4][4] = "84";
Upvotes: 0
Views: 84
Reputation: 234795
You can do this by writing a custom comparator object:
Comparator<String[]> comp = new Comparator<>() {
@Override
public int compare(String[] student1, String[] student2) {
// comparison logic goes here
// return:
// a negative number if student1 goes before student2
// 0 if student1 is tied with student2
// a positive number if student1 goes after student2
}
};
Then you can sort with:
Arrays.sort(aryStudents, comp);
As other answers have suggested, you would probably be better off defining a Student
class to encapsulate the data for a student. You could then define the class itself to implement Comparable<Student>
(in which case the comparison logic would be fixed) or you can define a Comparator<Student>
object as above (except the arguments would be (Student student1, Student student2)
instead of (String[] student1, ...)
).
EDIT: Since you asked in a comment, here's how the comparison logic might work. If the scores are always two digits and padded with zero (so, for instance, a score of 8 would be "08"
), then you can simply use
return student1[3].compareTo(student2[3]);
This does a lexicographic comparison, which does what you want. If a score of 8 would be represented by "8"
, then that won't work. Instead, you will have to parse the scores as int
values and do the comparison. As long as the integers are only two or three digits long, a nice shortcut would be:
return Integer.parseInt(student1[3]) - Integer.parseInt(student2[3]));
I've used 3 as the index of the last element because in your question code you've declared the array for each student to be length 4.
Upvotes: 2
Reputation: 42
Be careful arrays first index starts with 0 th element.Your solution will be throw a StringIndexOutOfBoundsException
.
Upvotes: -1
Reputation: 56
the array is for strings, but you can convert it to an int using Integer.parseInt. Then iterate over all subarrays and check the last value; put that in a comparator
public int compare(String[] s1, String[] s2) {
int last = Integer.parseInt(s1[s1.length - 1]);
int last2 = Integer.parseInt(s2[s2.length - 1]);
return Integer.compare(last, last2);
}
Untested because I'm on mobile and cannot test.
Upvotes: 0
Reputation: 93842
Use an oriented object approach.
Create a class Student
, each student having a name and a list of marks. Then create an array of Student and sort it providing a custom comparator. You could achieve the sort with your 2-dimensional array, but you add a level of complexity.
public class Student {
private String name;
private List<Integer> grades;
//Constructor, getter, setter, equals, hashcode, etc.
}
Java is an object-oriented programming language so make use of it.
Upvotes: 2