如何使用ArrayList或其他工具依据指定单词对句子进行排序?
Absolutely! You can sort the sentences based on your predefined target word order by creating custom sorting logic that maps each sentence to its target word and uses a priority list to determine the order. Here's how to do it step-by-step:
Step 1: Define the Priority Order
First, create a priority mapping for your target words to assign numerical weights—this makes comparing them straightforward. For your desired order (eventually → reality → seed), we’ll assign lower numbers to words that should come first:
Map<String, Integer> wordPriority = new HashMap<>(); wordPriority.put("eventually", 0); wordPriority.put("reality", 1); wordPriority.put("seed", 2);
Step 2: Load Sentences into an ArrayList
Add all your sentences to an ArrayList<String>:
ArrayList<String> sentences = new ArrayList<>(); sentences.add("01. reality ~ [ rēˈalətē ] ≠ [ unreality ] - TV is used as an escape from reality."); sentences.add("02. seed ~ [ sēd ] ≠ [ ] - A packet of sunflower seeds."); sentences.add("03. eventually ~ [ iˈvenCHo͞oəlē ] ≠ [ ] - He eventually escaped and made his way back to England.");
Step 3: Sort with a Custom Comparator
Use Collections.sort() with a custom comparator that extracts the target word from each sentence and uses our priority map to sort:
Collections.sort(sentences, (s1, s2) -> { // Extract target word: split after the number/period, take the first word before "~" String word1 = s1.split("\\. ")[1].split(" ~")[0]; String word2 = s2.split("\\. ")[1].split(" ~")[0]; // Compare based on priority values return wordPriority.get(word1).compareTo(wordPriority.get(word2)); });
Step 4: Output the Sorted Result
After sorting, iterating over the ArrayList will give you the desired order:
for (String sentence : sentences) { System.out.println(sentence); }
Expected Output:
- eventually ~ [ iˈvenCHo͞oəlē ] ≠ [ ] - He eventually escaped and made his way back to England.
- reality ~ [ rēˈalətē ] ≠ [ unreality ] - TV is used as an escape from reality.
- seed ~ [ sēd ] ≠ [ ] - A packet of sunflower seeds.
Alternative: Python Implementation (Using List and Custom Key)
If you prefer Python, you can achieve the same result with a list and the sorted() function using a custom key:
# Define priority order word_priority = {"eventually": 0, "reality": 1, "seed": 2} # List of sentences sentences = [ "01. reality ~ [ rēˈalətē ] ≠ [ unreality ] - TV is used as an escape from reality.", "02. seed ~ [ sēd ] ≠ [ ] - A packet of sunflower seeds.", "03. eventually ~ [ iˈvenCHo͞oəlē ] ≠ [ ] - He eventually escaped and made his way back to England." ] # Sort using the priority map sorted_sentences = sorted(sentences, key=lambda s: word_priority[s.split(". ")[1].split(" ~")[0]]) # Print result for sentence in sorted_sentences: print(sentence)
This will produce the exact sorted order you’re looking for. The core idea is extracting the target word from each sentence and using the predefined priority to determine the sort sequence.
内容的提问来源于stack exchange,提问作者Taha Sami

