• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

Customizer Sort of List

 
Greenhorn
Posts: 23
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have a list of string items in List object as follows

A-B-100.55.23D
A-B-100.50.23A
A-B-100.55.23A
A-B-100.55.23D
A-B-100.60.23A
A-B-101.50.23D
A-B-101.50.23E
A-B-101.50.21A
A-B-101.50.21E
A-B-100.60.23D

Expected is wanted to sort until the number changes after 100 value or next series. and last one to be sorted by desc. One the number changes again it will sort the number but last one would be sorted by desc

A-B-100.55.23D
A-B-100.50.23A
A-B-100.55.23D
A-B-100.55.23A
A-B-100.60.23A
A-B-101.50.21E
A-B-101.50.21A
A-B-101.50.23D
A-B-101.50.23E
A-B-100.60.23D

How to do this in java? I List.sort or Array.sort cannot be used for this as it can sort the entire as ascending or descending only.

Can someone suggest the best way to achieve this? Any code sample would be really helpful.

Thanks
 
Saloon Keeper
Posts: 12990
281
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to split the list in parts that you want to sort, and then sort each part individually according to your requirements.
 
Joseph Bran
Greenhorn
Posts: 23
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Stephen,

I'm struggling with the code snippet. Please help me



Expected Output:

A-ABC-100.55.23D
A-ABC-101.55.23D
A-ABC-100.55.23E
A-ABC-100.55.23D
A-ABC-100.55.23B
A-ABC-100.55.23A
A-ABC-100.56.23D
A-ABC-100.55.24D
A-ABC-100.55.24A
A-ABC-101.55.23E
A-ABC-101.55.23D
A-ABC-100.57.24D


I am not sure how to get the expected output

a. iterate until the number changes before first dot
b. collect all the details and walk through the collection
c. iterate until the number changes before second dot
d. collect all the details and walk through the collection
e. final sort by descending order

It is very confusing and not sure how to achieve it
 
Rancher
Posts: 4307
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I'm struggling ...


Please explain.

Please wrap the posted code in code tags:  Select the code and press  the Code button.
 
Saloon Keeper
Posts: 8220
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

a. iterate until the number changes before first dot
b. collect all the details and walk through the collection
c. iterate until the number changes before second dot
d. collect all the details and walk through the collection
e. final sort by descending order

This could be implemented by
making a sublist of all the lines where the characters match up until the second ".".
Sort the sublist in descending order and output it.
Repeat the process until the input list is exhausted.

 
Saloon Keeper
Posts: 4497
166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Agreed.

As an alternative, you could add a groupnumber to the strings in some form, and sort first on that groupnumber.
For instance, if you have
101
100
102
100
100

you could create
101 1
100 2
102 3
100 4
100 4

Then you can sort the complete list with a suitable Comparator.
 
Carey Brown
Saloon Keeper
Posts: 8220
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

 
Joseph Bran
Greenhorn
Posts: 23
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Carey Brown.

Can you please send the sortDescending() and getGroup() method code as well

Thanks
 
Carey Brown
Saloon Keeper
Posts: 8220
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For sorting, see this Sort List Descending.

For getGroup(), you should be able to put that together yourself using String#indexOf(char,int) and String#substring(int,int). The comment says what it is doing.
 
Piet Souris
Saloon Keeper
Posts: 4497
166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To expand a little on the article pointed to by Carey:

say you have strings in the form "A-B-100:55:23D" and you want to sort them by the second number (55) and then by the last part (23D) in descending order.

As written, to do so you need to split the string in its components. Now, to sort a List of such Strings in the required way, you can write a dedicated Comparator for it. for instance:

and then, if you have your list, you can do:

This is just to illustrate how it could be done. You should never use raw Collections, and a big disadvantage here is that if you have to sort the strings a, b and c, then a gets compared to b and to c, and b gets compared to c. That means that each string is decomposed twice. But anyway, I hope you get the idea!
 
Carey Brown
Saloon Keeper
Posts: 8220
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
With the code I already supplied the sorting is easier than that. The sort only applies to groups and the leading characters within a  group are identical so there's no need to break it into components. So a simple sort on String is all that is necessary, it just has to be descending.
 
Piet Souris
Saloon Keeper
Posts: 4497
166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got the impression that sorting was to be done on two fields within a group. See OP'S opening post. But then, I could be wrong.
 
Carey Brown
Saloon Keeper
Posts: 8220
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are correct but the two fields work together so a group is defined by all characters up to the second period (.).

Edit:
And by sorting only a group at a time there's no need to break the String into separate fields, you can just do a simple String sort.
 
Carey Brown
Saloon Keeper
Posts: 8220
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Only one line so could have been in-lined.

 
straws are for suckers. tiny ads are for attractive people.
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic