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

Greenhorn
Posts: 23
1
• Number of slices to send:
Optional 'thank-you' note:
Hi,

I am having list of string values which follows the following pattern: Part1+Part2+Part3+Par4+Part5

Ex:

Part1=0 or 1 only
Part2=Before first dot = A-ABC-100 (Max Number supported is 500 i.e A-ABC-500)
Part3=Before second dot (Can be Alphabets or Number)
Part4=Before third dot (Can be Alphabets of Number)
Part5=After third dot (Can be Alphabets of Number)

Need:

Part1=Descending Order
Part2=Sorting not required
Part3=Grouping for the same number (Ex: all 100.55 is one group. Need to pick all 100.55
into group.)
Part4=Ascending if part1 is 0 and Descending if part1 is 1
Part5=Ascending if 0 and Descending if 1

Sample Input:

0-A-ABC-100.55.23.D
1-A-ABC-101.55.23.D
0-A-ABC-100.200.29.A
1-A-ABC-100.51.23.D
0-A-ABC-100.55.23.E
1-A-ABC-100.200.23.B
1-A-ABC-100.56.23.D
1-A-ABC-100.55.24.A
0-A-ABC-100.55.24.D
1-A-ABC-101.51.23.F
1-A-ABC-101.151.235.D
0-A-ABC-101.55.23.E
1-A-ABC-100.200.29.D
1-A-ABC-100.51.24.D
1-A-ABC-100.151.242.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-100.55.23.21
1-A-ABC-100.55.23.20

Expected Output

1-A-ABC-100.55.23.F
1-A-ABC-100.55.21.E
1-A-ABC-100.55.23.21
1-A-ABC-100.55.23.20
0-A-ABC-100.55.23.D
0-A-ABC-100.55.23.E
0-A-ABC-100.55.23.22
1-A-ABC-101.55.23.D
0-A-ABC-101.55.23.E
1-A-ABC-100.200.29.D
0-A-ABC-100.200.29.A
1-A-ABC-100.51.23.F
1-A-ABC-101.51.23.D
1-A-ABC-100.200.23.B
1-A-ABC-100.151.242.D

code which is not working to solve the above...

Please help me on how to achieve the expected output. The above logic initially works for a simple sort, then the sorting becomes cumbersome with sorting and grouping so not sure how to achieve this.

Thanks

Saloon Keeper
Posts: 4511
166
• Number of slices to send:
Optional 'thank-you' note:
If I understand you correctly, you must group your Strings on the basis of and that 100.55 and on the first character(0 or 1), since that first character descides the sorting.

I created a SpecialString for each original String, and after grouping, I could sort the SpecialStrings with the Comparator defined. That gave correct outcomes (although the Comparator might not be 100% correct). I would take the same approach here. Since the sorting can now be done ascending and descending, I would define an extra Comparator for when part1 is 0 or 1.

And the grouping can be done via a combined String part1+"100.55" (or differentparts, and to each group you supply the correct Comparator. As said, that's how I would handle this.

Joseph Bran
Greenhorn
Posts: 23
1
• Number of slices to send:
Optional 'thank-you' note:
Thanks a lot, Piet for the effort. However how to call this...Also i don't like to send any parameter..The last line throws error since it expects the parameters t and i which i don't know. Please advise.

import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;

public class SortOrder {
public static void main(String[] args) {
List<String> s = new ArrayList<String>();
s.sort(new SpecialString());
}
}

Joseph Bran
Greenhorn
Posts: 23
1
• Number of slices to send:
Optional 'thank-you' note:
Peir..Also, updated the expected output more clearly matching the input.

Piet Souris
Saloon Keeper
Posts: 4511
166
• Number of slices to send:
Optional 'thank-you' note:
Right, well it is kinda complicated. But first: you wrote that you must group your String list, based on that 100.55 part, and also that within such a group, sorting can be done both ascending and decreasing, depending on that first 0 or 1. Correct me if I got that wrong. So, that grouping must first be done. Next, each subgroup must be sorted, and for that, each group needs a dedicated Comparator. So my suggestion is:
in the SpecialString class, have a method 'getComparator', and that method sends a correct Comparator depending on that first 0 or 1.
Then for each subgroup, get that Comparator and sort the group.

Complicated, but your sorting requirement is not easy as well. But before I go on: in your previous topic Carey Brown was very much involved, so I would like his opinion first now, see what he thinks. I think he will be around any time soon now.

Joseph Bran
Greenhorn
Posts: 23
1
• Number of slices to send:
Optional 'thank-you' note:
Thanks Piet. I understand the reason for confusion is because expected output is very confusing and minor mistakes. below are clear details

1) 0 Or 1 - Indictor where 1 is should be on top always
2) later after 0 or 1 until second dot and before second dot comes the group info

following are the groups in the above for more clarity

A-ABC-100.55
A-ABC-101.55
A-ABC-100.200
A-ABC-100.51
A-ABC-100.56
A-ABC-101.151
A-ABC-100.151

3) The groups should be in the same above order

4) All the elements to the corresponding group to be added

5) Sort the next value. If 0 indicator it would come as last in the same group. If 1 it would come as first in the same group

6) Sort by the last value. Same as #5

Expected Output:

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

Saloon Keeper
Posts: 8237
71
• Number of slices to send:
Optional 'thank-you' note:
Need to be precise here

Regular expression:

0-A-ABC-100.55.23.D   (0)  (A-ABC)  (100.55)  (23.D)
1-A-ABC-101.55.23.D  (0)  (A-ABC)  (101.55)  (23.D)
0-A-ABC-100.200.29.A  (0)  (A-ABC)  (100.200)  (29.A)

Group(3) in regex-speak, in the first line would be (100.55). This would be the data group id that subgroups are made out of  for sorting. The sort order will be based on regex-group(1).

Questions:
For a given sort group (regex-group(3)) will the following hold true:
* The first and second regex-groups will be identical.
* We don't care what follows regex-group(3).
* The number of characters before the period will always be 3.
* The number of characters after the period will be 2 or 3.

Joseph Bran
Greenhorn
Posts: 23
1
• Number of slices to send:
Optional 'thank-you' note:

Carey Brown
Saloon Keeper
Posts: 8237
71
• Number of slices to send:
Optional 'thank-you' note:
This code loads the data from a text file and parses it, just as a first step.
Output:
Some of the groups show both a '0' and '1' sorting within a group. How can this be?

Piet Souris
Saloon Keeper
Posts: 4511
166
• Number of slices to send:
Optional 'thank-you' note:

Carey Brown wrote:For a given sort group (regex-group(3)) will the following hold true:
* The first and second regex-groups will be identical.

I see this in the opening post:
0-A-ABC-100.55.23.22
0-A-ABC-100.55.22.D
1-A-ABC-100.55.23.21
1-A-ABC-100.55.23.20

That's why I suggested to subgroup on both the A_ part and the first character. to sort them in a different way

Joseph Bran
Greenhorn
Posts: 23
1
• Number of slices to send:
Optional 'thank-you' note:
Hi Carey Brown,

Thank you so much but i get a different result. Can you please share the txt file which you have used for the above program.

Thank you

Joseph Bran
Greenhorn
Posts: 23
1
• Number of slices to send:
Optional 'thank-you' note:
I understand Campbell Ritchie. Sorry for editing and thought it would give clarity. Carey Brown, Please kindly help me with your text file to review. Thanks a lot.

Carey Brown
Saloon Keeper
Posts: 8237
71
• Number of slices to send:
Optional 'thank-you' note:
1-A-ABC-100.55.24.A
1-A-ABC-100.55.23.F
1-A-ABC-100.55.23.21
1-A-ABC-100.55.23.20
1-A-ABC-100.55.21.E
0-A-ABC-100.55.22.D
0-A-ABC-100.55.23.D
0-A-ABC-100.55.23.E
0-A-ABC-100.55.23.22
0-A-ABC-100.55.24.D
1-A-ABC-101.55.23.D
0-A-ABC-101.55.23.E
1-A-ABC-100.200.29.D
1-A-ABC-100.200.23.B
0-A-ABC-100.200.29.A
1-A-ABC-100.51.24.D
1-A-ABC-100.51.23.F
1-A-ABC-100.56.23.D
1-A-ABC-101.51.23.D
1-A-ABC-101.151.235.D
1-A-ABC-100.151.242.D

Joseph Bran
Greenhorn
Posts: 23
1
• Number of slices to send:
Optional 'thank-you' note:
Hi Carey Brown,

I got a wrong result. Can you please share the input file which you have used?

My Output:

(1)  (A-ABC-101.55)  (23.D)
(0)  (A-ABC-100.200)  (29.A)
(1)  (A-ABC-100.51)  (23.D)
(0)  (A-ABC-100.55)  (23.E)
(1)  (A-ABC-100.200)  (23.B)
(1)  (A-ABC-100.56)  (23.D)
(1)  (A-ABC-100.55)  (24.A)
(0)  (A-ABC-100.55)  (24.D)
(1)  (A-ABC-101.51)  (23.F)
(1)  (A-ABC-101.151)  (235.D)
(0)  (A-ABC-101.55)  (23.E)
(1)  (A-ABC-100.200)  (29.D)
(1)  (A-ABC-100.51)  (24.D)
(1)  (A-ABC-100.151)  (242.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-100.55)  (23.21)
(1)  (A-ABC-100.55)  (23.20)

My code

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SortOfSort2
{
private static final Pattern    PAT =
Pattern.compile( "(0|1)-([^-]+-[^-]+-[0-9a-zA-Z]{3}\\.[0-9a-zA-Z]{2,3})\\.(.*)" );

public static void main( String[] args ) throws Exception
{
List<String>  list = new ArrayList<>();

String      prevGroup = "";

for( String s : list )
{
Matcher     m = PAT.matcher( s );

if( ! m.matches() )
throw new Exception( "NO MATCH: \"" + s + "\"" );

if( ! prevGroup.equals( m.group(2)  ) )
System.out.println(  );
prevGroup = m.group(2);

System.out.printf( "(%s)  (%s)  (%s)%n",
m.group(1), m.group(2), m.group(3) );

}
}

static List<String> loadList( String fname ) throws FileNotFoundException
{
List<String>  list = new ArrayList<>();
try( Scanner in = new Scanner( new File( fname ) ) )
{
while( in.hasNextLine() )
}
return list;
}
}

Carey Brown
Saloon Keeper
Posts: 8237
71
• Number of slices to send:
Optional 'thank-you' note:
This is the file. I copied it from your post.

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

Carey Brown
Saloon Keeper
Posts: 8237
71
• Number of slices to send:
Optional 'thank-you' note:
Changed file to:
Sample Input:

0-A-ABC-100.55.23.D
1-A-ABC-101.55.23.D
0-A-ABC-100.200.29.A
1-A-ABC-100.51.23.D
0-A-ABC-100.55.23.E
1-A-ABC-100.200.23.B
1-A-ABC-100.56.23.D
1-A-ABC-100.55.24.A
0-A-ABC-100.55.24.D
1-A-ABC-101.51.23.F
1-A-ABC-101.151.235.D
0-A-ABC-101.55.23.E
1-A-ABC-100.200.29.D
1-A-ABC-100.51.24.D
1-A-ABC-100.151.242.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-100.55.23.21
1-A-ABC-100.55.23.20

New Output:
(0)  (A-ABC-100.55)  (23.D)

(1)  (A-ABC-101.55)  (23.D)

(0)  (A-ABC-100.200)  (29.A)

(1)  (A-ABC-100.51)  (23.D)

(0)  (A-ABC-100.55)  (23.E)

(1)  (A-ABC-100.200)  (23.B)

(1)  (A-ABC-100.56)  (23.D)

(1)  (A-ABC-100.55)  (24.A)
(0)  (A-ABC-100.55)  (24.D)

(1)  (A-ABC-101.51)  (23.F)

(1)  (A-ABC-101.151)  (235.D)

(0)  (A-ABC-101.55)  (23.E)

(1)  (A-ABC-100.200)  (29.D)

(1)  (A-ABC-100.51)  (24.D)

(1)  (A-ABC-100.151)  (242.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-100.55)  (23.21)
(1)  (A-ABC-100.55)  (23.20)

Carey Brown
Saloon Keeper
Posts: 8237
71
• Number of slices to send:
Optional 'thank-you' note:
(0)  (A-ABC-100.55)  (23.67)
(0)  (A-ABC-100.55)  (23.22)
(0)  (A-ABC-100.55)  (22.D)
(1)  (A-ABC-100.55)  (23.21)
(1)  (A-ABC-100.55)  (23.20)

I'm not clear on this. It looks like all these entries are a part of one group (true?). Yet some get sorted ascending and some descending. I'm not clear how this is supposed to be sorted by both ascending and descending. Please clarify.

Piet Souris
Saloon Keeper
Posts: 4511
166
• Number of slices to send:
Optional 'thank-you' note:
This is what I had in mind:

I see in your examples that group 100.55 comes before 100.200. Is that correct? Or is that irrelevant?

Edit: oops. Forgot in the result to map the specialstrings back to the original strings!

Carey Brown
Saloon Keeper
Posts: 8237
71
• Number of slices to send:
Optional 'thank-you' note:
My understanding is that within groups sorting will take place, other than that, the groups themselves must remain in the order encountered.

Carey Brown
Saloon Keeper
Posts: 8237
71
• 1
• Number of slices to send:
Optional 'thank-you' note:
This is an interesting problem because the required logic is not typical, but also describing the requirements in English is turning out to be an almost impossible task. We seem to be mostly beating our heads against the requirements. There must be a better way. Note that have example inputs and outputs is useful for verifying requirements but they are no substitute for succintly written requirements. Even requirements written in pseudo code or as bullet points would be an improvement.

Piet Souris
Saloon Keeper
Posts: 4511
166
• Number of slices to send:
Optional 'thank-you' note:

Carey Brown wrote:My understanding is that within groups sorting will take place, other than that, the groups themselves must remain in the order encountered.

Yes, that was in the previous topic. But OP writes in his opening post:

Part3=Grouping for the same number (Ex: all 100.55 is one group. Need to pick all 100.55
into group.)

And you are absolutely right: one does not encounter such sorting on a day-to-day basis (fortunately)

Carey Brown
Saloon Keeper
Posts: 8237
71
• Number of slices to send:
Optional 'thank-you' note:
Ah, to clarify...
The sample input/output I posted: The output is only the PARSING, there is no sorting or other processing taking place. This is just to verify with you that these are the correctly parsed fields as you see it.

Carey Brown
Saloon Keeper
Posts: 8237
71
• Number of slices to send:
Optional 'thank-you' note:
Piet can you shed any light on this? What am I missing?

Carey Brown wrote:(0)  (A-ABC-100.55)  (23.67)
(0)  (A-ABC-100.55)  (23.22)
(0)  (A-ABC-100.55)  (22.D)
(1)  (A-ABC-100.55)  (23.21)
(1)  (A-ABC-100.55)  (23.20)

I'm not clear on this. It looks like all these entries are a part of one group (true?). Yet some get sorted ascending and some descending. I'm not clear how this is supposed to be sorted by both ascending and descending. Please clarify.

Piet Souris
Saloon Keeper
Posts: 4511
166
• Number of slices to send:
Optional 'thank-you' note:
I think it is a mistake. According to the 0, the sorting should be done ascending on the last field, and for the 1 descending. But it seems the sorting is descending in both cases. Waiting for OP to explain.

Maybe it is a good idea to ask OP where on earth this is coming from.

Joseph Bran
Greenhorn
Posts: 23
1
• Number of slices to send:
Optional 'thank-you' note:
Hi Carey Brown/Piet,

Sorry for putting both of you in trouble..Since it is complex it would be confusing...

Carey Brown, Your input is correct but the output is wrong. Below is the actual  input and expected output.

Actual Input:

0-A-ABC-100.55.23.D
1-A-ABC-101.55.23.D
0-A-ABC-100.200.29.A
1-A-ABC-100.51.23.D
0-A-ABC-100.55.23.E
1-A-ABC-100.200.23.B
1-A-ABC-100.56.23.D
1-A-ABC-100.55.24.A
0-A-ABC-100.55.24.D
1-A-ABC-101.51.23.F
1-A-ABC-101.151.235.D
0-A-ABC-101.55.23.E
1-A-ABC-100.200.29.D
1-A-ABC-100.51.24.D
1-A-ABC-100.151.242.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-100.55.23.21
1-A-ABC-100.55.23.20

Expected Ouput:

Expected Output:

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

Below are the groups in sequence order

A-ABC-100.55
A-ABC-101.55
A-ABC-100.200
A-ABC-100.51
A-ABC-100.56
A-ABC-101.151
A-ABC-100.151

1) 0 Or 1 - Indictor where 1 is should be on top always

2) later after 0 or 1 until second dot and before second dot comes the group info

3) The groups should be in the order as above

4) All the elements to the corresponding group to be added

5) Sort the next value. If 0 indicator it would come as last in the same group. If 1 it would come as first in the same group

6) Sort by the last value.

Please kindly let me know if you have any queries.

Piet Souris
Saloon Keeper
Posts: 4511
166
• Number of slices to send:
Optional 'thank-you' note:
Don't worry: Carey and I like some braincrackers, so pleisure is all ours!

A question:

The fact that 100.55 comes before 100.200 comes before 100.51 is duw to the fact that in the input we encounter this order?

Joseph Bran
Greenhorn
Posts: 23
1
• 1
• Number of slices to send:
Optional 'thank-you' note:
Hi Piet,

100.55 comes first since this group comes first. Whichever group comes first it takes the priority...Then whichever group comes next...In the above example 100.55 comes first, so 101.55 is the second first group..then the next group is 100.200 comes first

if the input is as follows

1-100.200.1.A
1-100.55.3.A
1-100.200.1.B
1-100.101.4.A
1-100.55.4.A

the group sequence would be in result as

1-100.200.1.B
1-100.200.1.A
1-100.55.4.A
1-100.55.3.A
1-100.101.4.A

Hope this gives clarity

Thanks a lot..

Carey Brown
Saloon Keeper
Posts: 8237
71
• Number of slices to send:
Optional 'thank-you' note:
What does this  mean?
1) 0 Or 1 - Indictor where 1 is should be on top always
On top of what?
You have multiple 1's, which should be on top of which?

Carey Brown
Saloon Keeper
Posts: 8237
71
• Number of slices to send:
Optional 'thank-you' note:

Joseph Bran wrote:100.55 comes first since this group comes first. Whichever group comes first it takes the priority...Then whichever group comes next...In the above example 100.55 comes first, so 101.55 is the second first group..then the next group is 100.200 comes first

if the input is as follows

1-100.200.1.A
1-100.55.3.A
1-100.200.1.B
1-100.101.4.A
1-100.55.4.A

the group sequence would be in result as

1-100.200.1.B
1-100.200.1.A
1-100.55.4.A
1-100.55.3.A
1-100.101.4.A

Applying just this logic I now get:

Carey Brown
Saloon Keeper
Posts: 8237
71
• Number of slices to send:
Optional 'thank-you' note:
How come you only get sort order 1's ?

Carey Brown
Saloon Keeper
Posts: 8237
71
• Number of slices to send:
Optional 'thank-you' note:
Can I ask, what is the practical application of this? If you sort a large amount of data this way it would be very difficult for a human to find a particular entry without a computer, and if you have a computer why sort it at all?

Piet Souris
Saloon Keeper
Posts: 4511
166
• Number of slices to send:
Optional 'thank-you' note:
Well, had another look at the group partitioning. I created therefore a special method 'getGroupComparator' and with this input (your ez\xample)

var s = new ArrayList<String>();

I get as a result the following group sequence:

[100.55, 101.55, 100.200, 100.51, 100.56, 101.51, 101.151, 100.151]

and the grouping with corresponding lists:

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]

(the lists are not sorted yet). Does that look okay?

Carey Brown
Saloon Keeper
Posts: 8237
71
• Number of slices to send:
Optional 'thank-you' note:
Ah, so within a group all the 1's come first followed by the 0's.

Piet Souris
Saloon Keeper
Posts: 4511
166
• Number of slices to send:
Optional 'thank-you' note:
I'm not sure, I assumed it.

Carey Brown
Saloon Keeper
Posts: 8237
71
• Number of slices to send:
Optional 'thank-you' note:
I've got some clean, but non-optimized code that produces this. Old-school, no streams or lambdas.

Piet Souris
Saloon Keeper
Posts: 4511
166
• 1
• Number of slices to send:
Optional 'thank-you' note:
Given OP's remark:

5) Sort the next value. If 0 indicator it would come as last in the same group. If 1 it would come as first in the same group
6) Sort by the last value.

I thought that a 0-group should be descending on the last field, and ascending if a 1-group. Burt re-reading and seeing your output, I think you are right, only simple sorting on the last field, simplifying things much. Well, I guess it comes to showing the code. I used Comparators and lambda's:

Joseph Bran
Greenhorn
Posts: 23
1
• Number of slices to send:
Optional 'thank-you' note:
Hi Piet,

The last code which you have done above is almost correct. except the last portion which needs to be ordered as per 1 or 0. Now all the grouping is correct

The expected output is

Expected Output:

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

-------------------

When 1 is coming, the last two portions should be on descending order..so   24.A should come 1st and 23.F comes next..for 0 record line everything is perfect. Can you please kindly check and let me know?

Carey brown, as you asked, yes within a group all the 1's come first followed by the 0's. Also, in that 1-last 2 values should be in descending order. for 1 last two values should be descending order. for 0 it should be ascending order as we got the output finally in the above last code by piet.

Thanks a lot.

Carey Brown
Saloon Keeper
Posts: 8237
71
• Number of slices to send:
Optional 'thank-you' note:
Joseph, have you been working on your own version of the code while Piet and I have been poking around with ours? Care to share what you've done?

Piet Souris
Saloon Keeper
Posts: 4511
166
• Number of slices to send:
Optional 'thank-you' note:
So I WAS right in the first place, after all?

As I said, that makes it a bit more complex, since the input has to be divided into corresponding subgroups and each subgroup gets sorted differently. Carey, do you agree?

I see this:

0-A-ABC-100.55.22.D
0-A-ABC-100.55.23.D
0-A-ABC-100.55.23.E
0-A-ABC-100.55.23.22
0-A-ABC-100.55.24.D

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