sorting string array based on delimiter

Discussion in 'Java' started by padmagvs, Aug 9, 2010.

  1. padmagvs

    padmagvs

    Joined:
    Aug 9, 2010
    Messages:
    1
    Hi ,

    I need to sort a string array based on delimiter

    example,
    have below values in string array
    cproj@@9.184.184.143@@mycon2@@dbo@@ABC,
    cproj@@9.184.184.143@@mycon2@@dbo@@B,
    cproj@@9.184.184.143@@mycon2@@dbo@@Ctable,
    cproj@@9.184.184.143@@mycon2@@dbo@@ZZ,
    cproj@@9.184.184.143@@mycon2@@dbo@@a,
    cproj@@9.184.184.143@@mycon2@@dbo@@katble,
    cproj@@9.184.184.143@@mycon2@@dbo@@mtable,
    cproj@@9.184.184.143@@mycon2@@dbo@@ztable

    I need output in sorted way case insensitive
    cproj@@9.184.184.143@@mycon2@@dbo@@a,
    cproj@@9.184.184.143@@mycon2@@dbo@@ABC,
    cproj@@9.184.184.143@@mycon2@@dbo@@B,
    cproj@@9.184.184.143@@mycon2@@dbo@@Ctable,
    cproj@@9.184.184.143@@mycon2@@dbo@@katble,
    cproj@@9.184.184.143@@mycon2@@dbo@@mtable,
    cproj@@9.184.184.143@@mycon2@@dbo@@ZZ,
    cproj@@9.184.184.143@@mycon2@@dbo@@ztable
     
    padmagvs, Aug 9, 2010
    #1
    1. Advertising

  2. padmagvs

    cuffJ

    Joined:
    Aug 15, 2010
    Messages:
    2
    Um...I recommend creating a compareTo method in a comparator that takes in the string and takes out the delimeters. Then you can just use Arrays.sort(ArrayName, ComparatorName).

    Ex:
    public DelimeterComparator implements Comparator<String>
    {
    public int compare(String anotherString, String aString){
    while(x>=0 && y>=0){
    int x=anotherString.indexOf(DELIMETER);
    int y=aString.indexOf(DELIMETER);
    if (x>=0 && y>=0){
    tempString1=anotherString.substring(0,x);
    tempString2=aString.substring(0,y);
    if (!tempString1.equals(tempString2))
    //Returns negative # if tempString1<tempString2, else positive number
    return tempString1.compareTo(tempString2);
    else{
    anotherString=anotherString.subString(x+DELIMETER.length());
    aString=aString.subString(y+DELIMETER.length());
    }
    }
    }
    //Strings are equal
    return 0;
    }
    private static final DELIMETER="@@";
    }
     
    cuffJ, Aug 16, 2010
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Pedro Rocha
    Replies:
    8
    Views:
    15,418
    Boudewijn Dijkstra
    Dec 8, 2004
  2. gbattine
    Replies:
    0
    Views:
    535
    gbattine
    Jun 19, 2006
  3. Prasanth
    Replies:
    4
    Views:
    416
    Prasanth
    Nov 22, 2008
  4. rhungund@gmail.com

    ASP Form Array - Delimiter Problem

    rhungund@gmail.com, Apr 6, 2005, in forum: ASP General
    Replies:
    2
    Views:
    125
    rhungund@gmail.com
    Apr 6, 2005
  5. Valentino Lun

    convert string to array with delimiter

    Valentino Lun, Nov 5, 2008, in forum: Ruby
    Replies:
    3
    Views:
    133
    Heesob Park
    Nov 5, 2008
Loading...

Share This Page