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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!