简体   繁体   中英

Sorting an array of sentence according to the original sentence array

I have an array of SENTENCE1 that contains a few sentences:

String[] SENTENCE1 = new String[]{
"This book is nice",
"I like it",
"I read them alot", 
"My favourite book", 
"I put it in a shelf"};

Next, I also have an array of sentences which is randomly obtained from SENTENCE1 array which is called SENTENCE2:

String[] SENTENCE2 = new String[]{
"I put it in a shelf" ,   
"I like it",
"My favourite book"};

How do I sort SENTENCE2 array according to the appearance sequence in SENTENCE1 so that the output in SENTENCE2 is:

I like it
My favourite book
I put it in a shelf

Anyway, I tried to do it and loop through them but it shows Arrays out of bound.

for (int g=0;g<SENTENCE2.length;g++){
    for (int o=0;o<SENTENCE1.length;o++)
    {
        if (SENTENCE2[g].contains(SENTENCE1[o])){
            System.out.println(SENTENCE2[g])
        }
    }
}

Thanks for the help.

Your code is correct, One thing is you don't need contains if String's exact matching is to be done. use equals

for(int i = 0; i < sent1.length; i++){
    for(int j = 0; j < sent2.length; j++){
        if(sent1[i].equals(sent2[j])){
            System.out.println(sent2[j]);
        }
    }
}

Always use camel casing in java for local variables

Try this.

    List<String> list = Arrays.asList(SENTENCE1);
    String[] sorted = Stream.of(SENTENCE2)
        .sorted(Comparator.comparing(s -> list.indexOf(s)))
        .toArray(String[]::new);
    System.out.println(Arrays.toString(sorted));
    // -> [I like it, My favourite book, I put it in a shelf]
 TreeMap<Integer, String> map = new TreeMap<>();

 for (int i=0;i<SENTENCE2.length;i++) {
     for (int j = 0; j < SENTENCE1.length; j++) {
         if (SENTENCE2[i].equals(SENTENCE1[j])) {
             map.put(j, SENTENCE2[i]);
         }
     }
 }

 map.values().toArray(SENTENCE2);

 for(String value: SENTENCE2){
     System.out.println(value);
 }

In TreeMap values will be sorted by key automatically.

If you happy to use external library like google guava . It can be easily done like below :

public static void main(String[] args) {
    String[] SENTENCE1 = new String[]{
        "This book is nice",
        "I like it",
        "I read them alot",
        "My favourite book",
        "I put it in a shelf"};

    String[] SENTENCE2 = new String[]{
        "I put it in a shelf",
        "I like it",
        "My favourite book"};

    Comparator<String> SENTENCE1_COMPARATOR = Ordering.explicit(Arrays.asList(SENTENCE1));
    Arrays.sort(SENTENCE2, SENTENCE1_COMPARATOR);
    System.out.println(Arrays.toString(SENTENCE2));
}

you can do like this:

    @Test
public void test() {
    String[] SENTENCE1 = new String[]{
            "This book is nice",
            "I like it",
            "I read them alot",
            "My favourite book",
            "I put it in a shelf"};
    String[] SENTENCE2 = new String[]{
            "I put it in a shelf",
            "I like it",
            "My favourite book"};


    System.out.println(SENTENCE1.length);
    for(int i= 0 ;i<SENTENCE1.length;i++){
        boolean flag = false;
        for(int j = 0; j<SENTENCE2.length;j++){
            if(SENTENCE2[j].equals(SENTENCE1[i])){
                flag = true;
                break;
            }
        }
        if(!flag){
            SENTENCE1[i]=null;
        }
    }

    for(int k=0;k<SENTENCE1.length;k++){
        if(SENTENCE1[k]!=null)
            System.out.println(SENTENCE1[k]);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM