• 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

Sorting & Grouping for the given List

 
Saloon Keeper
Posts: 4511
166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And a good morning to you all! Just got up.
 
Greenhorn
Posts: 23
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
0-A-ABC-100.55.23.E - 0-A-ABC-100.55.23.22

Either alphabetical should come in ascending order for this or Number should come in ascending order. either way is fine. But for zero the last two digits should be in ascending order in the internal group. All should follow the same pattern.

Ex:

0-A-ABC-100.55.23.E
0-A-ABC-100.55.24.A
0-A-ABC-100.55.24.B
0-A-ABC-100.55.23.1
0-A-ABC-100.55.23.2
0-A-ABC-100.55.23.3
0-A-ABC-100.55.24.1

Please let me know if need more detail.
 
Saloon Keeper
Posts: 8243
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
Ditto...

Piet Souris wrote:Why is 0-A-ABC-100.55.23.22 considered to be larger than 0-A-ABC-100.55.23.E?

 
Carey Brown
Saloon Keeper
Posts: 8243
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 wrote:0-A-ABC-100.55.23.E - 0-A-ABC-100.55.23.22

Either alphabetical should come in ascending order for this or Number should come in ascending order. either way is fine. But for zero the last two digits should be in ascending order in the internal group. All should follow the same pattern.

Ex:

0-A-ABC-100.55.23.E
0-A-ABC-100.55.24.A
0-A-ABC-100.55.24.B
0-A-ABC-100.55.23.1
0-A-ABC-100.55.23.2
0-A-ABC-100.55.23.3
0-A-ABC-100.55.24.1

Please let me know if need more detail.


How do you figure that these are in ascending order?
23.E
24.A
24.B
23.1
23.2
23.3
24.1
 
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
0-A-ABC-100.55.23.E
0-A-ABC-100.55.24.A
0-A-ABC-100.55.24.B
0-A-ABC-100.55.23.1
0-A-ABC-100.55.23.2
0-A-ABC-100.55.23.3
0-A-ABC-100.55.24.1

Because 23 comes first before 24. The output for the above will be

0-A-ABC-100.55.23.E
0-A-ABC-100.55.23.1
0-A-ABC-100.55.23.2
0-A-ABC-100.55.23.3
0-A-ABC-100.55.24.A
0-A-ABC-100.55.24.B
0-A-ABC-100.55.24.1
 
Carey Brown
Saloon Keeper
Posts: 8243
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
1-A-ABC-100.55.24.A    ::  [0]  (1)  (A-ABC-100.55)  (24)  (A)
1-A-ABC-100.55.23.21   ::  [0]  (1)  (A-ABC-100.55)  (23)  (21)
1-A-ABC-100.55.23.20   ::  [0]  (1)  (A-ABC-100.55)  (23)  (20)
0-A-ABC-100.55.22.D    ::  [0]  (0)  (A-ABC-100.55)  (22)  (D)
0-A-ABC-100.55.23.D    ::  [0]  (0)  (A-ABC-100.55)  (23)  (D)
0-A-ABC-100.55.23.E    ::  [0]  (0)  (A-ABC-100.55)  (23)  (E)
0-A-ABC-100.55.23.22   ::  [0]  (0)  (A-ABC-100.55)  (23)  (22)
0-A-ABC-100.55.23.67   ::  [0]  (0)  (A-ABC-100.55)  (23)  (67)
0-A-ABC-100.55.24.D    ::  [0]  (0)  (A-ABC-100.55)  (24)  (D)

1-A-ABC-101.55.23.D    ::  [1]  (1)  (A-ABC-101.55)  (23)  (D)
0-A-ABC-101.55.23.E    ::  [1]  (0)  (A-ABC-101.55)  (23)  (E)

1-A-ABC-100.200.29.D   ::  [2]  (1)  (A-ABC-100.200)  (29)  (D)
1-A-ABC-100.200.23.B   ::  [2]  (1)  (A-ABC-100.200)  (23)  (B)
0-A-ABC-100.200.29.A   ::  [2]  (0)  (A-ABC-100.200)  (29)  (A)

1-A-ABC-100.51.24.D    ::  [3]  (1)  (A-ABC-100.51)  (24)  (D)
1-A-ABC-100.51.23.D    ::  [3]  (1)  (A-ABC-100.51)  (23)  (D)

1-A-ABC-100.56.23.D    ::  [4]  (1)  (A-ABC-100.56)  (23)  (D)

1-A-ABC-101.51.23.F    ::  [5]  (1)  (A-ABC-101.51)  (23)  (F)

1-A-ABC-101.151.235.D  ::  [6]  (1)  (A-ABC-101.151)  (235)  (D)

1-A-ABC-100.151.242.D  ::  [7]  (1)  (A-ABC-100.151)  (242)  (D)
 
Carey Brown
Saloon Keeper
Posts: 8243
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
Could you provide an input/output set that will test all the scenarios?
 
Piet Souris
Saloon Keeper
Posts: 4511
166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, got it (touchwood).

With this input list:
List<String> s = new ArrayList<String>();
      s.add("0-A-ABC-100.55.23.D");
      s.add("1-A-ABC-101.55.23.D");
      s.add("0-A-ABC-100.200.29.A");
      s.add("1-A-ABC-100.51.23.D");
      s.add("0-A-ABC-100.55.23.E");
      s.add("1-A-ABC-100.200.23.B");
      s.add("1-A-ABC-100.56.23.D");
      s.add("1-A-ABC-100.55.24.A");
      s.add("0-A-ABC-100.55.24.D");
      s.add("1-A-ABC-101.51.23.F");
      s.add("1-A-ABC-101.151.235.D");
      s.add("0-A-ABC-101.55.23.E");
      s.add("1-A-ABC-100.200.29.D");
      s.add("1-A-ABC-100.51.24.D");
      s.add("1-A-ABC-100.151.242.D");
      s.add("0-A-ABC-100.55.23.67");
      s.add("0-A-ABC-100.55.23.22");
      s.add("0-A-ABC-100.55.22.D");
      s.add("1-A-ABC-100.55.23.21");
      s.add("1-A-ABC-100.55.23.20");

I get the following outcome: (inclusive in-between results)

the groups in encounting order:
{101.51=5, 100.200=2, 100.51=3, 101.151=6, 101.55=1, 100.151=7, 100.55=0, 100.56=4}
***********************************8
created map before sorting the sublists
100.55-1 - [1-A-ABC-100.55.24.A, 1-A-ABC-100.55.23.21, 1-A-ABC-100.55.23.20]
100.55-0 - [0-A-ABC-100.55.23.D, 0-A-ABC-100.55.23.E, 0-A-ABC-100.55.24.D, 0-A-ABC-100.55.23.67, 0-A-ABC-100.55.23.22, 0-A-ABC-100.55.22.D]
101.55-1 - [1-A-ABC-101.55.23.D]
101.55-0 - [0-A-ABC-101.55.23.E]
100.200-1 - [1-A-ABC-100.200.23.B, 1-A-ABC-100.200.29.D]
100.200-0 - [0-A-ABC-100.200.29.A]
100.51-1 - [1-A-ABC-100.51.23.D, 1-A-ABC-100.51.24.D]
100.56-1 - [1-A-ABC-100.56.23.D]
101.51-1 - [1-A-ABC-101.51.23.F]
101.151-1 - [1-A-ABC-101.151.235.D]
100.151-1 - [1-A-ABC-100.151.242.D]
********************************
map after sorting of the sublists:
100.55-1 - [1-A-ABC-100.55.23.20, 1-A-ABC-100.55.23.21, 1-A-ABC-100.55.24.A]
100.55-0 - [0-A-ABC-100.55.24.D, 0-A-ABC-100.55.23.E, 0-A-ABC-100.55.23.D, 0-A-ABC-100.55.23.67, 0-A-ABC-100.55.23.22, 0-A-ABC-100.55.22.D]
101.55-1 - [1-A-ABC-101.55.23.D]
101.55-0 - [0-A-ABC-101.55.23.E]
100.200-1 - [1-A-ABC-100.200.23.B, 1-A-ABC-100.200.29.D]
100.200-0 - [0-A-ABC-100.200.29.A]
100.51-1 - [1-A-ABC-100.51.23.D, 1-A-ABC-100.51.24.D]
100.56-1 - [1-A-ABC-100.56.23.D]
101.51-1 - [1-A-ABC-101.51.23.F]
101.151-1 - [1-A-ABC-101.151.235.D]
100.151-1 - [1-A-ABC-100.151.242.D]
*****************************8
result list
1-A-ABC-100.55.23.20
1-A-ABC-100.55.23.21
1-A-ABC-100.55.24.A
0-A-ABC-100.55.24.D
0-A-ABC-100.55.23.E
0-A-ABC-100.55.23.D
0-A-ABC-100.55.23.67
0-A-ABC-100.55.23.22
0-A-ABC-100.55.22.D
1-A-ABC-101.55.23.D
0-A-ABC-101.55.23.E
1-A-ABC-100.200.23.B
1-A-ABC-100.200.29.D
0-A-ABC-100.200.29.A
1-A-ABC-100.51.23.D
1-A-ABC-100.51.24.D
1-A-ABC-100.56.23.D
1-A-ABC-101.51.23.F
1-A-ABC-101.151.235.D
1-A-ABC-100.151.242.D
BUILD SUCCESSFUL (total time: 0 seconds)

I showed you a complete code that was not 100% correct. Did you give it a look, and could you follow it?
 
Carey Brown
Saloon Keeper
Posts: 8243
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
I get the same results as you Piet, but my code is not as elegant. For instance, here's my compareTo() method.

 
Piet Souris
Saloon Keeper
Posts: 4511
166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Elegance is in the eyes of the beholder...

Your code is very clear. It is just that I like the Comparator class a lot.

One question (I've seen it before): you have

Isn't that dangerous? I use in this case, to be save and because I find that very clear:
 
Carey Brown
Saloon Keeper
Posts: 8243
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

Piet Souris wrote:


After I looked at it, I would have written:
 
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
Thank you so much...Carey Brown/Piet, can you please share the full code snippet to test?
 
Piet Souris
Saloon Keeper
Posts: 4511
166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is still a bit too early. Carey and I having a discussion about the last groups say 23.E and 23.22
How to sort in case of a 1-group and in case of a 0-group. Your examples showed both at the same time, so that is still unclear.

But is this sorting a real-world thing, and if so, can you explain, or is it just a way to pass the time?
 
Joseph Bran
Greenhorn
Posts: 23
1
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Piet,

Thank you so mich. Take your own time. The compareTo not sure how it would solve the problem since the first group should be on top of the list. Also each batch group should be sequential as per the list.

I think we should approach in a different way.

It's a real problem.

TravellingOrNot-CountryCode-CityCode-TrainCode-Compartment-Division-Tier

Thanks.
 
Piet Souris
Saloon Keeper
Posts: 4511
166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Joseph,

thanks for the information.

both Carey and I have a solution, albeit that we used a slightly different way.

For me, I made use of so called Comparators.
You see, for each String in the input I created a key like 100.55-1 (group - sorting indicator), and I created a Comparator based on this key that exactly splitted up the List into sublists, in the correct order. Then it was just a matter of sorting the sublists, using another Comparator that depended on the 0 or 1, and adding the sublists together. As I showed, my outcome looks correct. You may want to have a look at the Comparator class.

As soon as Carey's problem is solved we will show our code.

 
Carey Brown
Saloon Keeper
Posts: 8243
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
How are you doing with your code Joseph? Needing any help with that?
 
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,

Thanks for asking. All these are strings are coming from an API and just wanted to sort before display and don't need much help related to the code as such.

Thanks a lot again.
 
Piet Souris
Saloon Keeper
Posts: 4511
166
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It has taken long enough. Here's my code: note thatthis code is NOT Thread-save!
 
Marshal
Posts: 26626
81
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A slight simplification:


 
Piet Souris
Saloon Keeper
Posts: 4511
166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's a lot shorter. But that code is generated by NetBeans, so it is just a mouseclick.

Edit: hmm, is that hashCode compatible with equals, I wonder?
 
Paul Clapham
Marshal
Posts: 26626
81
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:hmm, is that hashCode compatible with equals, I wonder?



The API documentation says "This method is useful for implementing Object.hashCode() on objects containing multiple fields" so I would assume it's a proper hashCode() method.
 
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 Piet,

Everything comes fine except each internal group is not sorted by ascending or descending based on 1 or 0.

1-A-ABC-100.55.24.A
1-A-ABC-100.55.23.21
1-A-ABC-100.55.23.20
0-A-ABC-100.55.22.D
0-A-ABC-100.55.23.D
.....
....
....

Thanks
 
Piet Souris
Saloon Keeper
Posts: 4511
166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you sure? If you run the code, you get this ouput:

0-A-ABC-100.55.23.D       - 1-A-ABC-100.55.23.20    
1-A-ABC-101.55.23.D       - 1-A-ABC-100.55.23.21    
0-A-ABC-100.200.29.A      - 1-A-ABC-100.55.24.A      
1-A-ABC-100.51.23.D       - 0-A-ABC-100.55.24.D      
0-A-ABC-100.55.23.E       - 0-A-ABC-100.55.23.E      
1-A-ABC-100.200.23.B      - 0-A-ABC-100.55.23.D      
1-A-ABC-100.56.23.D       - 0-A-ABC-100.55.23.67    
1-A-ABC-100.55.24.A       - 0-A-ABC-100.55.23.22    
0-A-ABC-100.55.24.D       - 0-A-ABC-100.55.22.D      
1-A-ABC-101.51.23.F       - 1-A-ABC-101.55.23.D      
1-A-ABC-101.151.235.D     - 0-A-ABC-101.55.23.E      
0-A-ABC-101.55.23.E       - 1-A-ABC-100.200.23.B    
1-A-ABC-100.200.29.D      - 1-A-ABC-100.200.29.D    
1-A-ABC-100.51.24.D       - 0-A-ABC-100.200.29.A    
1-A-ABC-100.151.242.D     - 1-A-ABC-100.51.23.D      
0-A-ABC-100.55.23.67      - 1-A-ABC-100.51.24.D      
0-A-ABC-100.55.23.22      - 1-A-ABC-100.56.23.D      
0-A-ABC-100.55.22.D       - 1-A-ABC-101.51.23.F      
1-A-ABC-100.55.23.21      - 1-A-ABC-101.151.235.D    
1-A-ABC-100.55.23.20      - 1-A-ABC-100.151.242.D    

The left column is the inputlist, the right column is the sorted list. This is just to check the outcome.

If you have a list of strings, then all you have to do is:
 
reply
    Bookmark Topic Watch Topic
  • New Topic