如何基于参数(布尔变量)选择条件对vector执行排序?
Hey there! Let's work through this conditional sorting problem together. I get that you already know how to sort with a single rule, so the main trick here is dynamically switching between two different sorting logics based on that boolean flag.
First, let's break down the approach—whether you're using standard sorting rules or custom ones like the one that gives your expected output [1 10 6 8 9 9], the core idea stays the same: pick the right sorting logic based on your boolean variable. Let's use Python for examples since it's straightforward for these tasks, but you can adapt this logic to other languages too.
1. Standard Ascending/Descending Sort (Basic Case)
If your "Rule 1" is standard ascending order and "Rule 2" is descending, here's a simple way to implement the conditional switch:
def conditional_sort(arr, use_rule1): # Apply Rule 1 (ascending) if the flag is True if use_rule1: return sorted(arr) # Otherwise apply Rule 2 (descending) else: return sorted(arr, reverse=True) # Test it out with your array original_arr = [1, 9, 6, 8, 9, 10] print(conditional_sort(original_arr, True)) # Output: [1, 6, 8, 9, 9, 10] print(conditional_sort(original_arr, False)) # Output: [10, 9, 9, 8, 6, 1]
2. Custom Rules (Matching Your Expected Output)
Your expected output [1 10 6 8 9 9] looks like a custom rule—from what I can tell, it places the smallest element first, largest element second, then keeps the rest of the elements in their original relative order. Let's define that as Rule 1, and say Rule 2 does the opposite (largest first, smallest second, rest in original order). Here's how to code that:
def rule1(arr): # Make a copy to avoid modifying the original array arr_copy = arr.copy() # Grab and remove the smallest value min_val = min(arr_copy) arr_copy.remove(min_val) # Grab and remove the largest value from the remaining elements max_val = max(arr_copy) arr_copy.remove(max_val) # Reconstruct the array with min first, max second, rest in original order return [min_val, max_val] + arr_copy def rule2(arr): arr_copy = arr.copy() max_val = max(arr_copy) arr_copy.remove(max_val) min_val = min(arr_copy) arr_copy.remove(min_val) # Reconstruct with max first, min second, rest in original order return [max_val, min_val] + arr_copy def conditional_sort(arr, use_rule1): # Switch between rules based on the boolean flag return rule1(arr) if use_rule1 else rule2(arr) # Test with your example original_arr = [1, 9, 6, 8, 9, 10] print(conditional_sort(original_arr, True)) # Output: [1, 10, 6, 8, 9, 9] (matches your expected result!) print(conditional_sort(original_arr, False)) # Output: [10, 1, 9, 6, 8, 9]
3. Generalizable Pattern for Any Rules
No matter what your specific sorting rules are, this pattern works:
- Define each rule separately: Keep each sorting logic as its own function (or lambda for simple cases) to keep your code clean and easy to update.
- Use a conditional to select the rule: A simple
if/elsecheck on your boolean variable lets you pick which rule to apply. - Preserve the original array: Always work on a copy if you don't want to alter the original data (like we did with
arr.copy()above).
If you prefer using lambdas for simpler rules, you can also do something like this (great for standard sorting tweaks):
original_arr = [1, 9, 6, 8, 9, 10] use_rule1 = True # Dynamically set sorting parameters reverse = not use_rule1 # Reverse if using Rule 2 sorted_arr = sorted(original_arr, reverse=reverse)
内容的提问来源于stack exchange,提问作者Dorian Gray

