Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Re-ordering list which has duplicate values

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Re-ordering list which has duplicate values

    I have a list with the following string values.
    [4|oo44|oo44|oo44|244, 2|oo|oo|oo|519,548, 3|oo33|oo33|oo33|024,900, 1|header1|id1|class1|466]

    Based on this input value, I have created a new list with the required values (first and last value in each item). So my final list has
    4:244
    2:519
    2:548 //2 is duplicated here since the input has two values related to the row number 2.
    3:024
    3:900 //3 is duplicated here since the input has two values related to the row number 3.
    1:466

    Now, I have to sort these items and then wherever I have duplicate values(in this case 2 and 3), those items should be re-numbered.
    After sorting I would get
    1:466 //should remain same in final list
    2:519 //should remain same in final list
    2:548 //should be changed to 3:548
    3:024 //should be changed to 4:024
    3:900 //should be changed to 5:900
    4:244 //should be changed to 6:244

    The expected output is
    1:466
    2:519
    3:548
    4:024
    5:900
    6:244

    Any help would be much appreciated.


  2. #2
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Re-ordering list which has duplicate values

    You could sort it once, then go over the entire list and relabel the entries in ascending order. That should not be too hard if you already managed to sort them.
    If your values are all strings you can use the method indexOf(char c) to get the position of the ':' character, and then use substring(int from, int to) to split the string into its 2 components.
    With Integer.parseInt(String s) you can convert a string to an int, then increment, and parse it back to your new string.

Similar Threads

  1. Store data from Excel to Database without duplicate values
    By vector_ever in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 1st, 2013, 04:12 PM
  2. To remove duplicate values from 2 big files
    By tcstcs in forum Java Theory & Questions
    Replies: 6
    Last Post: March 1st, 2012, 07:12 PM
  3. How to make set to allow duplicate values... Please help me!!!
    By JavaKiddo in forum Collections and Generics
    Replies: 9
    Last Post: January 4th, 2012, 08:22 AM
  4. Removing Duplicate Values From an Array
    By nicsa in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 30th, 2011, 07:55 PM
  5. Eliminating duplicate values
    By Harry_ in forum Collections and Generics
    Replies: 7
    Last Post: November 9th, 2009, 06:35 AM

Tags for this Thread