How can you sort this
String [][] Array11= {{array[i]},{array2[i]},{array3[i]}};
brief explanation about what im trying to work: array contains student ID number array2 contains student name array3 contains student course. All of them are STRING ARRAYS
if i input:
ID number: 5 name: A course: CS
ID number: 3 Name: B course: IT
i should sort it by its ID number so the output would look like this
ID Number Name Course 3 B IT 5 A CS
or if you have any other suggestions on how to make it work, please help me. :)
You should replace your 2D string array with a 1D array of Student
s, where you create a Student
class that has the attributes "id", "name", and "course".
Then you create a class that implements Comparator<Student>
, say StudentComparator
. Its compare
method should return a negative number, 0
, or a positive number, depending on whether the first Student
compares to the second Student
as less than, equal to, or greater than.
You need to create a custom Comparator
.
Check out Column Comparator it provides two approaches:
ColumnComparator
as a reusuable Comparator simply by specify the index of the column to sort on. Or a better design is to create a custom Student Object to store the properties of the Student (Id, Name, Course), then you can use the Bean Comparator , to sort on any of the properties in the class.
The way you are storing the data is not appropriate. I would recommend you to define a class for your Students that implements Comparable. Then overide compareTo() function such that it compares by the course id
public class Student implements Comparable<Student >
{
public int id;
public String name;
public String course;
public int compareTo(Student s)
{
if(s ==null)
return 1;
return id - s.id;
}
}
Now you can store all your students in the ArrayList as follows :
Student [] studentsList = new Student [sizeOfStudent];
Now you can just sort using Arrays utility sort method :
Arrays.sort(studentsList);
Note: If you want to use a different data structure, then I would recommend using ArrayList instead of an Array.
The best way to do this is to stop pretending that you are using C and start actually using Java.
step 1: create an object that contains student id, student name, and student course.
step 2: store these objects in a collection (array, list, other) that makes sense.
step 3; sort the collections.
here is some code:
public class StudentBlam implements Comparable<StudentBlam>
{
private final String course;
private final String id;
private final String name;
public StudentBlam(
final String course,
final String id,
final String name)
{
this.course = course;
this.id = id;
this.name = name;
}
@Override
public int compareTo(final StudentBlam other)
{
return id.compareTo(other.getId());
}
public String getCourse()
{
return course;
}
public String getId()
{
return id;
}
public String getName()
{
return name;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.