Results 1 to 6 of 6
  1. #1
    GavinCash is offline Member
    Join Date
    Oct 2010
    Posts
    10
    Rep Power
    0

    Default Comparing Names and Sorting

    Heya folks, another question here.

    If I was to take in an array of names (a set of strings that have spaces inside of them - i.e. John James, Mark Matthews, etc.), how would I be able to sort those based on the last name?

    I've thought of a simple Arrays.sort(names); but that is first-difference only.

    Thanks,
    Gavin

  2. #2
    somanayr is offline Member
    Join Date
    May 2010
    Posts
    27
    Rep Power
    0

    Default

    for(int i = 0; i < names.length; i++){
    String lastName = names[i].split(" ")[1];
    //do stuff with lastName
    }

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Use the other version of Arrays.sort- the one that takes a Comparator argument.

    If you haven't used Comparator before you might want to read this JavaWorld article which explains how to go beyond the "natural" sorting order to create additional sort orderings.

  4. #4
    GavinCash is offline Member
    Join Date
    Oct 2010
    Posts
    10
    Rep Power
    0

    Default

    Thank you both for the quick replies.

    Somanayr - I'll try your bit first and, so long as it works and I can manage to fill in that blank, it'll do what I need. Edit: Alright, so I see that that gives me the last names. But what do I do from there with it? Would I just compareTo, and do an if - else statement that re-arranges the names (which would be quite... confusing) or would I simply just bash my head against my computer and hope that, through osmosis, I can understand the Java language?

    pbrockway2 - I'll read up on those two links and find out if I can't streamline, or increase efficiency at a later date, thank you.
    Last edited by GavinCash; 10-14-2010 at 01:46 AM.

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

    Default

    Quote Originally Posted by GavinCash View Post
    Thank you both for the quick replies.

    Somanayr - I'll try your bit first and, so long as it works and I can manage to fill in that blank, it'll do what I need. Edit: Alright, so I see that that gives me the last names. But what do I do from there with it? Would I just compareTo, and do an if - else statement that re-arranges the names (which would be quite... confusing) or would I simply just bash my head against my computer and hope that, through osmosis, I can understand the Java language?

    pbrockway2 - I'll read up on those two links and find out if I can't streamline, or increase efficiency at a later date, thank you.
    For my 2 cents, ignore Somanayr's suggestion. He means well, but it's not how you should sort this. pbrockway on the other hand will lead you to a solution.

  6. #6
    GavinCash is offline Member
    Join Date
    Oct 2010
    Posts
    10
    Rep Power
    0

    Default

    Alrighty, thanks once again Fubarable. I had to submit the assignment already, but I'll be able to use it properly for the next assignment. I guess this can be changed to (Solved)

Similar Threads

  1. Names pipes in java
    By Zept in forum Networking
    Replies: 2
    Last Post: 08-06-2011, 08:20 PM
  2. Set names utf8
    By initmax in forum JDBC
    Replies: 1
    Last Post: 11-22-2009, 10:29 AM
  3. Replies: 8
    Last Post: 02-20-2009, 12:40 PM
  4. Drive names
    By alwz_nikhil in forum New To Java
    Replies: 0
    Last Post: 11-28-2007, 10:04 AM
  5. how to get the names of the files
    By mary in forum Advanced Java
    Replies: 2
    Last Post: 08-05-2007, 04:01 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
  •