• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Compare and Sort a List

New Here ,
Oct 05, 2007 Oct 05, 2007

Copy link to clipboard

Copied

I have this list called productColors:
<cfset productColors = "White, Translucent, Slate, Sand, Moss, Merlot, Latte, Black">

And, I have a dynamic list that is not sorted correctly and is a subset of the list above.
So, it might look like this:
"Latte, Black, Sand"

I'd like to end up with this list sorted like the one above:
"Sand, Latte, Black"

How might I write a function to do this?
TOPICS
Advanced techniques

Views

411

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Oct 05, 2007 Oct 05, 2007

Copy link to clipboard

Copied

Not sure of a function to do this with. Are these color values in a table? Maybe you could add a sort (sequence) field for each color.

Maybe add the colors to an array and have a sort value for each, then sort the array on that.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Oct 05, 2007 Oct 05, 2007

Copy link to clipboard

Copied

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 05, 2007 Oct 05, 2007

Copy link to clipboard

Copied

Let's call that these lists L1 (big list), L2 (subset), L3, and L4

<cfset L3 = "">
<cfset L4 = "">

<cfloop list = "#L2#" item = TheItem>
<cfset L3 = ListAppend(L3, ListFind(L1, TheItem))>
</cfloop>
<cfset L3 = ListSort(L3)>

<cfloop list = "#L3#" item = TheItem>
<cfset L4 = ListAppend(L4, ListGetAt(L1, TheItem))>
</cfloop>

or something like that

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 05, 2007 Oct 05, 2007

Copy link to clipboard

Copied

Thanks very much for the response and help getting started. Here's what I've come up with that seems to work:

<cffunction name="cleanAndSortColorList" access="public" output="true">
<!--- The list we want to clean up --->
<cfargument name="L1" type="string" required="yes"/>
<!--- The complete sorted list --->
<cfset L2 = "White,Translucent,Slate,Sand,Moss,Merlot,Latte,Black">
<!--- Use a struct to build a set of the list elements without duplicates--->
<cfset set = StructNew()>
<cfloop index="elem" list="#L1#">
<cfset set[elem] = "">
</cfloop>
<!--- Convert the set back to a list --->
<cfset L1 = StructKeyList(set)>

<cfset L3 = "">
<cfset L4 = "">
<cfloop list = "#L1#" index = TheItem>
<cfset L3 = ListAppend(L3, ListFind(L2, TheItem))>
</cfloop>
<cfset L3 = ListSort(L3,"numeric")>
<cfloop list = "#L3#" index = TheItem>
<cfset L4 = ListAppend(L4, ListGetAt(L2, TheItem))>
</cfloop>
<cfreturn L4>
</cffunction>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 05, 2007 Oct 05, 2007

Copy link to clipboard

Copied

LATEST
What's the purpose of converting the list to a structure and then back to a list?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation