This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to get sub lists of a list

 
pjoisha
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I'm using Arrays.sort(Object[], Comparator) to help sort some of my objects. However, I now have a need to create sublists off of a sorted list
For example I have a list of following objects
Person[] person = new Person[3];
// The Person constructor takes
// LastName, FirstName
person[0] = new Person(Goodyear, Nancy);
person[1] = new Person(Clark,Stanley);
person[2] = new Person(Goodyear, Elvis);
I sort them
Array.sort(person, Person.LastNameComparator);
The list ends up being
Clark, Stanley
Goodyear, Nancy
Goodyear, Elvis
However, what I want the sort interface to return is a an array of sub lists (each sub list has only duplicates in them) as follows
SubList1: Clark, Stanley
SubList1: Goodyear, Nancy
Goodyear, Elvis
Are there any Java APIs or some other techniques available to do this?
Thanks in advance
PJ
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have always used something akin to the following:
 
pjoisha
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Joel
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic