LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Need Algorithm to Arrange List

Solved!
Go to solution

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:

rol.png

It just seems like there ought to be a better way.

"If you weren't supposed to push it, it wouldn't be a button."
0 Kudos
Message 1 of 8
(1,698 Views)

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)

0 Kudos
Message 2 of 8
(1,670 Views)

@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

"If you weren't supposed to push it, it wouldn't be a button."
0 Kudos
Message 3 of 8
(1,662 Views)

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.

 

altenbach_0-1622225633618.png

 

0 Kudos
Message 4 of 8
(1,650 Views)

@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.

 

altenbach_0-1622225633618.png

 


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.

"If you weren't supposed to push it, it wouldn't be a button."
0 Kudos
Message 5 of 8
(1,639 Views)

@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?

0 Kudos
Message 6 of 8
(1,635 Views)
Solution
Accepted by paul_cardinale

I'd use a simple keyed sort.

TwoListStableSort.png

Message 7 of 8
(1,632 Views)

@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.

"If you weren't supposed to push it, it wouldn't be a button."
0 Kudos
Message 8 of 8
(1,544 Views)