Joseph Bran

Greenhorn
+ Follow
since May 12, 2021
Cows and Likes
Cows
Total received
1
In last 30 days
1
Total given
0
Likes
Total received
3
Received in last 30 days
3
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Joseph Bran

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
3 days ago
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.
4 days ago
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.
5 days ago
Thank you so much...Carey Brown/Piet, can you please share the full code snippet to test?
5 days ago
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
6 days ago
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.
6 days ago
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.
6 days ago
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..
1 week ago
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.

Thanks in advance
1 week ago
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<>();
       list.add("0-A-ABC-100.55.23.D");
       list.add("1-A-ABC-101.55.23.D");
       list.add("0-A-ABC-100.200.29.A");
       list.add("1-A-ABC-100.51.23.D");
       list.add("0-A-ABC-100.55.23.E");
       list.add("1-A-ABC-100.200.23.B");
       list.add("1-A-ABC-100.56.23.D");
       list.add("1-A-ABC-100.55.24.A");
       list.add("0-A-ABC-100.55.24.D");
       list.add("1-A-ABC-101.51.23.F");
       list.add("1-A-ABC-101.151.235.D");
       list.add("0-A-ABC-101.55.23.E");
       list.add("1-A-ABC-100.200.29.D");
       list.add("1-A-ABC-100.51.24.D");
       list.add("1-A-ABC-100.151.242.D");
       list.add("0-A-ABC-100.55.23.67");
       list.add("0-A-ABC-100.55.23.22");
       list.add("0-A-ABC-100.55.22.D");
       list.add("1-A-ABC-100.55.23.21");
       list.add("1-A-ABC-100.55.23.20");

       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() )
               list.add( in.nextLine() );
       }
       return list;
   }
}
1 week ago
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.
1 week ago
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
1 week ago
you are absolutely right in your questions. Please kindly go-ahead and help me with your code.

1 week ago
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
1 week ago
Peir..Also, updated the expected output more clearly matching the input.

Please kindly let me know.
1 week ago