Solved

Comparing 2 Lists Coldfusion

Posted on 2016-10-08
8
65 Views
Last Modified: 2016-10-09
I have 2 lists with the same numbered elements, just in the 2nd list one list element has moved positions. I need to know how to find the original position of the moved list element (from List1) and the position it moved to (in List2) using Coldfusion. Here is an example

<List1 = '1,2,3,4,5'>
<List2 = '2,3,1,4,5'>

I need to know that "1" in List1 moved to the 3rd position in List2. Couple things to know

1. Each list will have the exact same numbers in them (ie list elements - 1,2,3,4,5), only in a different order

2. Only one list element will move, ie 1 moved to the 3rd position, no other list elements would change.

3. A list element can move forward or backwards, so the "1" could move the the 3rd position or the "5" could have moved to the 2nd position.
0
Comment
Question by:seeraig
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 52

Accepted Solution

by:
_agx_ earned 500 total points
ID: 41835529
Couple questions

2. Only one list element will move, ie 1 moved to the 3rd position, no other list elements would change.

In the example above, both "1" and "2" changed positions. Is that a typo?

3. A list element can move forward or backwards,

FWIW, you can't reliably determine an item was moved backward .. not from only the two lists anyway. The most you can do is determine that 2 or more elements have moved or swapped places.  For example:


Results:
struct
VALUE 	1                      <=== This is the value in the original or "FROM" position
FROM 	1                      <=== Original list position 1
TO 	        3                      <=== Moved to this list position 3

Open in new window


Code:

<cfset List1 = "1,2,3,4,5">
<cfset List2 = "2,3,1,4,5">

<cfset result = {}>
<cfset Array1 = listToArray(List1)>
<cfloop from="1" to="#arrayLen(Array1)#" index="oldPos">
      <!--- find this value in the other list --->
      <cfset newPos = listFindNoCase(list2, Array1[oldPos])>
      <!--- if it moved, store values and exit --->
      <cfif oldPos neq newPos>
            <cfset result = { from=oldPos, to=newPos, value=Array1[oldPos]} >
            <cfbreak>
      </cfif>
</cfloop>

<cfdump var="#result#">
0
 

Author Comment

by:seeraig
ID: 41835558
thanks for your reply. yes i guess when i say only one element will move, that is not really true, the other elements are moving in relation to the one that actually moved. is there no way to know if an element moved backwards? for example

<cfset List1 = "1,2,3,4,5">
<cfset List2 = "5,1,2,3,4">

Here I would need to know that the 5 is the one that actually moved, the others are just moving in relation to the one that moved. but using your code it shows the "1" moved from the 1st position to the 2nd position

FROM       1
TO               2
VALUE       1
0
 
LVL 52

Expert Comment

by:_agx_
ID: 41835568
Edit:

Unfortunately not. It's impossible to derive which element moved, and in which direction, if all you have is the 2 lists.  For example, if the new order were:

                    <List2 = '5,2,3,4,1'>

It could be that "1" was moved forward to the fifth position, or ... that "5" was moved backward to the first position.  However, unless you know the element that was moved, It's impossible to tell which.  If the lists were manipulated client side, like with drag and drop or something, you could capture the moved element before or during the move.  But without that piece of information, you can't really derive it after the fact. Not from just the two lists.
0
The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

 

Author Comment

by:seeraig
ID: 41836039
thanks again for your help on this. it's funny you mention drag and drop because that's what this is all about. So if you can just show me how to capture the moved element then I will be able to update things as I need to. I'm using this script below (the jquery version), what code do I need to capture the element that moved? The code automatically submits on drop event. The ajax goes to a coldfusion url that processes it, so I just need a way to capture the moved element into a coldfusion variable

https://davidwalsh.name/demo/drag-drop-sort-save-jquery.php
0
 
LVL 52

Expert Comment

by:_agx_
ID: 41836167
UPDATE:
it's funny you mention drag and drop because that's what this is all about.

Ok, but why do you need to know the direction items moved? If this is just straight db storage, the typical method is to either update everything OR delete/re-insert. Then order or direction changes aren't an issue.  Granted, updating only the records that moved can be slightly more efficient, in some cases, but it's also more complex and error prone.
My preference is the update all / delete/reinsert approach.

I'm using this script below (the jquery version)
   
If you still want to go the other direction, I'm not familiar with the plugin, but a quick search says you can hook into the drop event

     $( ".selector" ).droppable({
          drop: function( event, ui ) {}
     });

Then inside the function get the id of the moved element using:

         $(ui.draggable).attr("id")

With the id, you can grab the value and store it in a hidden field. Then use it identify the direction:
<cfset List1 = "1,2,3,4,5">
<cfset List2 = "5,2,3,4,1">
<cfset movedItem = "5">

<cfset oldPos = listFindNoCase(list1, movedItem)>
<cfset newPos = listFindNoCase(list2, movedItem)>
<cfset direction = (oldPos lt newPos ? "forward" : "backward")>
<cfoutput>
  #movedItem# moved #direction# from #oldPos# to #newPos#
</cfoutput>

Open in new window

0
 

Author Comment

by:seeraig
ID: 41836209
omg you're right, i was making this way more complicated than I needed to. I just updated all elements based off of the new sort list and it works fine, no issues.
0
 

Author Closing Comment

by:seeraig
ID: 41836210
fast to respond and very helpful in alternatives ways of doing it.
0
 
LVL 52

Expert Comment

by:_agx_
ID: 41836271
Glad to help :)
0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The technique is by far very Simple! How we can export the ColdFusion query results to DOC file?  Well before writing this I researched a lot in Internet but did not found a good Answer anyways!  So i thought now i should share my small snippet w…
Today, I was working on some optimization and spam-stopping techniques when I encountered Ben Nadel's post to reduce spam feature using Math (http://www.bennadel.com/blog/197-How-I-Stop-Spammers-On-My-ColdFusion-Blog.htm). While this method is not o…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question