0

Please pardon my understanding towards priority Queue and Comparator in Java.
It seems ,I am able to implement basic comparator for Priority Queue based on some sort order.
But I am not able to come up with something for the below scenario :
1. Given a list of Files with name convention xx_yy_zz.dat .<br/>
2.xx,yy,zz can be from 00-50 <br/>
3.I need to process the files with xx=30 first,xx=35 second xx=40 third and then the rest.<br/>

Since I have limited knowledge with Priority Queue ,I tried to implement it which i was able to sort but only in asc or desc value of xx which was not the requirement.
My approach was

put the list of file names in priority Queue ,split the filename on regex "_" then compare the first index of split array using comparator based on it values but as expected i failed miserably since my requirement was something different

Please share some ideas/approach.

It seems sadly ,I am not able to come up with the a required comparator for my case .
Nevertheless thanking you in anticipation

2
  • How should "the rest" be sorted? Can it be in any order or does it have to be in a specific order? Commented Aug 20, 2020 at 20:03
  • Thank you for your time.there is no such rule for others as such .but to be safe lexicographic ordering is prefered . Commented Aug 20, 2020 at 20:06

2 Answers 2

1

You can use simple if statements inside the compare() method to check if one string starts with "30" and the other does not. Then you know that this string must come before the other one. You run the following if statements like this on the first part of the filenames:

  1. Are they the same?
  2. Is the left one 30?
  3. Is the right one 30?
  4. Is the left one 35?
  5. Is the right one 35?
  6. Is the left one 40?
  7. Is the right one 40?

The comparator might look like this:

public int compare(String a, String b) {
    String[] splitA = a.split("_");
    String[] splitB = b.split("_");

    if (splitA[0].equals(splitB[0])) {
        return 0;
    }
    if (splitA[0].equals("30")) {
        return -1;
    }
    if (splitB[0].equals("30")) {
        return 1;
    }
    if (splitA[0].equals("35")) {
        return -1;
    }
    if (splitB[0].equals("35")) {
        return 1;
    }
    if (splitA[0].equals("40")) {
        return -1;
    }
    if (splitB[0].equals("40")) {
        return 1;
    }
    return 0;
}

With the following test source code:

System.out.println(Arrays.toString(data));
Arrays.sort(data, new SpecialComparator());
System.out.println(Arrays.toString(data));

You might get an output like this (depending on the data array):

[30_45_35.dat, 00_12_34.dat, 35_50_20.dat, 40_03_05.dat, 33_28_14.dat,
 30_16_31.dat, 20_29_23.dat, 24_41_29.dat, 30_49_18.dat, 40_12_13.dat]

[30_45_35.dat, 30_16_31.dat, 30_49_18.dat, 35_50_20.dat, 40_03_05.dat,
 40_12_13.dat, 00_12_34.dat, 33_28_14.dat, 20_29_23.dat, 24_41_29.dat]

(new lines added for clarity)

As you see you have the 30s first, then the only 35 second, then the 40s third and after that all the remaining stuff. You might want to use compareTo() on the strings in case the compareTo method would return 0 to get better "sub sorting" of strings, which would be equal based on this basic sorting above.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks a lot.i am very new to comparator .it worked as expected .one doubt is that I tried to add the data list to a priorty queue,apply same comparator ,n poll data from it .the order is changed.it is due to more sub string sorting ?.when I am calling poll, element should be removed n again remaning all heapified based on the same comparator .so why is ressult using priority queue fetching different result(not lexicographic) in this case.
@ArchiacCoder The complete order might be different when you don't add the additional lexicographic sorting of equal elements. But the overall ordering should still be that the 30s come first, then the 35s, then the 40s and then the remaining stuff.
Thanks for info ! noted !
0

May be I'm not understand what exactly you need... but simply try this code and it sort me all strings if they has two digits on the begining

    public static void main(String[] args) {



        PriorityQueue<String> q = new PriorityQueue<String>((first, second) -> {
            return Integer.parseInt(first.substring(0, 2)) - Integer.parseInt(second.substring(0, 2));
            //and if you want to reverse order, simply add "-" like this:
            //return -(Integer.parseInt(first.substring(0, 2)) - Integer.parseInt(second.substring(0, 2)));
        });


        q.add("23lk");
        q.add("22lkjl");
        q.add("45ljl");

        for(String str : q) {
            System.out.println(str);
        }
    }
}

adn output

22lkjl
23lk
45ljl

If this not solution, please explain problem with more details, may be I or anybody else will help you.

1 Comment

Thanks for your time.now add to your list 10abv,50abc,100aac, where string can be anything. Now I want order of retrieval should be 50abc,100abc,10abc,45ljl,22lkjl,23lk.. this is what I want given the queue...where the string can be anything

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.