Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By Fubarable

Thread: Sorting by Last Name

  1. #1
    burrish is offline Member
    Join Date
    Sep 2011
    Location
    Texas
    Posts
    17
    Rep Power
    0

    Default 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?

  2. #2
    mwr1976 is offline Senior Member
    Join Date
    Oct 2011
    Posts
    106
    Rep Power
    0

    Default 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

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.
    camickr likes this.

  4. #4
    burrish is offline Member
    Join Date
    Sep 2011
    Location
    Texas
    Posts
    17
    Rep Power
    0

    Default Re: Sorting by Last Name

    Quote Originally Posted by Fubarable View Post
    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.
    Thank you both for your replies!

    Fubarable, what method should I be thinking about when swapping the first and last name? Once it splits, should I swap them using some type of loop that switches their position in the array while keeping them associated, correctly?

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Sorting
    By amzers in forum NetBeans
    Replies: 1
    Last Post: 10-04-2011, 06:06 PM
  2. C# Sorting
    By larry_d1990 in forum New To Java
    Replies: 8
    Last Post: 01-27-2011, 09:51 AM
  3. Sorting
    By Freakzoyd in forum New To Java
    Replies: 3
    Last Post: 01-21-2011, 02:02 AM
  4. little help with sorting
    By drgnfire25 in forum New To Java
    Replies: 3
    Last Post: 01-15-2011, 09:23 PM
  5. sorting
    By jot321 in forum New To Java
    Replies: 18
    Last Post: 10-02-2008, 10:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •