Question: Correct the following code so that the objects in the array persons will be sorted based on their name: public class Example { public static
Correct the following code so that the objects in the array persons will be sorted based on their name: public class Example { public static void main(String[] args) { Person[] persons = {new Person("Sam", 3), new Person("Ann", 4), new Person("Rita", 1)}; java.util.Arrays.sort(persons); System.out.println( java.util.Arrays.toString(persons)); } } class Person{ private String name; private int id; Person(String name, int id) { this.name = name; this.id = id; } public String getName(){ return name;} public int getId() {return id;} public String toString() {return name + " " + id;} }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
