Results 1 to 5 of 5
Thread: Sorting by Last Name
- 10-15-2011, 05:44 AM #1
Member
- Join Date
- Sep 2011
- Location
- Texas
- Posts
- 17
- Rep Power
- 0
Sorting by Last Name
Yes, this is as basic as it gets, but I'm not finding the information that I want. Let's say that "John Smith" is the string in a single element of an array, and I want to sort by last name. I guess splitting the string at the space wouldn't help since it would separate the first and last name? I'm trying to figure out how to start alphabetizing everything after the space in a string. What string method do I need to be thinking of in a situation like this?
- 10-15-2011, 05:52 AM #2
Senior Member
- Join Date
- Oct 2011
- Posts
- 106
- Rep Power
- 0
Re: Sorting by Last Name
I would use the indexOf method for the space in between the names. Then the charAt(indexOf +1) method would return the first letter of the last name. Which you could just
iterate through each letter of the last name by incrementing the indexOf method.
hope it helps
-
Re: Sorting by Last Name
A couple of suggestions:
1) split the String and create a new String that is lastName, firstName, and sort those,
2) create a class with two (or more) fields for the Name Strings, say called MyName, and have it implement Comparable<MyName>, give it a compareTo method that sorts on the last name, and sort it that way.
3) Then figure out what you're going to do about common but non-standard names that wouldn't work for your task, such as Claus von Bülow and Ludwig van Beethoven.
- 10-15-2011, 05:29 PM #4
Member
- Join Date
- Sep 2011
- Location
- Texas
- Posts
- 17
- Rep Power
- 0
Re: Sorting by Last Name
-
Re: Sorting by Last Name
Myself, I'd go for creating a class that holds the first name and last name and that implements Comparable.
Similar Threads
-
Sorting
By amzers in forum NetBeansReplies: 1Last Post: 10-04-2011, 06:06 PM -
C# Sorting
By larry_d1990 in forum New To JavaReplies: 8Last Post: 01-27-2011, 09:51 AM -
Sorting
By Freakzoyd in forum New To JavaReplies: 3Last Post: 01-21-2011, 02:02 AM -
little help with sorting
By drgnfire25 in forum New To JavaReplies: 3Last Post: 01-15-2011, 09:23 PM -
sorting
By jot321 in forum New To JavaReplies: 18Last Post: 10-02-2008, 10:30 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks