05-28-2021 12:14 PM - edited 05-28-2021 12:39 PM
Looking for cleanest way to do this. I have 2 lists (1D arrays of strings): List_1 and List_2. Some items are on List_1 only, some are on List_2 only, some are on both lists. I want to rearrange the items in List_2, such that, as much as possible, the order matches List_1.
Here's what I've come up with:
It just seems like there ought to be a better way.
Solved! Go to Solution.
05-28-2021 12:38 PM - edited 05-28-2021 12:41 PM
Can you explain in more detail what "order matches List_1" means. Should the matching items all occupy the same indices in the result, just in a different order? Should they try to match the index they had in list 1? Should the result start out with all elements that match both lists? Are the lists sorted? Can the lists contain duplicates? Are the lists of similar size? If not, which one is typically larger?
I think it would help if you could give a small example and expected outcome. For example:
List_1: A X D F K
List_2: F D Y A B R M
Desired Result?
(EDIT: ahh, I wrote above before you attached the snipped. I'll have a look)
05-28-2021 12:53 PM - edited 05-28-2021 12:55 PM
@altenbach wrote:
Can you explain in more detail what "order matches List_1" means. Should the matching items all occupy the same indices in the result, just in a different order? Should they try to match the index they had in list 1? Should the result start out with all elements that match both lists? Are the lists sorted? Can the lists contain duplicates? Are the lists of similar size? If not, which one is typically larger?
I think it would help if you could give a small example and expected outcome. For example:
List_1: A X D F K
List_2: F D Y A B R M
Desired Result?
(EDIT: ahh, I wrote above before you attached the snipped. I'll have a look)
No duplicates; not sorted. When an items in List_2 aren't in List_1 their final position should be just after the preceding item that is in List_1.
Desired result from above example: A B R M D Y F
05-28-2021 01:14 PM - edited 05-28-2021 01:14 PM
OK, I misunderstood.
Here's my solution that keeps all unique elements of list_2 in original order, but sorts the items that also occur in list 1 according to list 1.
05-28-2021 01:21 PM
@altenbach wrote:
OK, I misunderstood.
Here's my solution that keeps all unique elements of list_2 in original order, but sorts the items that also occur in list 1 according to list 1.
Not quite what I'm looking for. Since Q is not in List_1, it should be placed after E, because E is in List_1 and Q is after E in List_2.
05-28-2021 01:24 PM - edited 05-28-2021 01:25 PM
@paul_cardinale wrote:
Not quite what I'm looking for. Since Q is not in List_1, it should be placed after E, because E is in List_1 and Q is after E in List_2.
But X is also not in list 1, so why should it stay in first position?
05-28-2021 01:25 PM
I'd use a simple keyed sort.
05-29-2021 08:02 PM
@altenbach wrote:
@paul_cardinale wrote:
Not quite what I'm looking for. Since Q is not in List_1, it should be placed after E, because E is in List_1 and Q is after E in List_2.But X is also not in list 1, so why should it stay in first position?
Here's my thinking: When an item is not in List_1, its final position (in the rearranged list) should be relative to whatever initially preceded it. Since nothing precedes X in List_2, nothing should precede it in the rearranged list.