How to pull list in alphabetical order using #listgetat? more details in post

New Here ,
Oct 29, 2014 Oct 29, 2014

Copy link to clipboard

Copied

I have images pulling into site but it seems to be pulling them based on most recent added, is there any way to pull by file name alphabetically?

I'm not too experienced with CF but I am good with HTML/CSS trying to fix some things on a friends site due to a runaway backend developer.

sorry if this doesn't make too much sense.

this is what it currently looks like

<div>

<img width="920" height="300" src="data/images/<cfoutput>#ListGetAt(pic, 1)#</cfoutput>" />

</div>

<div>

<img width="920" height="300" src="data/images/<cfoutput>#ListGetAt(pic, 2)#</cfoutput>" />

</div>

...

Views

203

Likes

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

correct answers 1 Correct Answer

Guide , Oct 29, 2014 Oct 29, 2014
Before you start using your list, convert it to an array using ListToArray(), then do an ArraySort() on it.  I'd also consider replacing your code with a loop that loops over your list or array and adds the <div> and <img> elements.  Then you can accommodate any number of pictures in a more dynamic fashion. <cfset PicArray = ArraySort( ListToArray( pic ), "text", "asc")> <cfloop array="#PicArray#" index="ThisPic">      <div>           <cfoutput>                <img width="920" height="300" src="...

Likes

Translate

Translate
Guide ,
Oct 29, 2014 Oct 29, 2014

Copy link to clipboard

Copied

Before you start using your list, convert it to an array using ListToArray(), then do an ArraySort() on it.  I'd also consider replacing your code with a loop that loops over your list or array and adds the <div> and <img> elements.  Then you can accommodate any number of pictures in a more dynamic fashion.

<cfset PicArray = ArraySort( ListToArray( pic ), "text", "asc")>

<cfloop array="#PicArray#" index="ThisPic">

     <div>

          <cfoutput>

               <img width="920" height="300" src="data/images/#ThisPic# />

          </cfoutput>

     </div>

</cfloop>

If the picture names have upper and lower case characters, and you want case insensitive sorting, just change "text" to "textnocase" in line 1 above.

-Carl V.

Likes

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 29, 2014 Oct 29, 2014

Copy link to clipboard

Copied

Thank you Carl, I think I might have gotten it to work using

     

     getlistat(ListSort(pic, "textnocase"), 1)

Im testing it now, if it doesn't work I will try your method

I am not a backend developer so the least I need to do here the safer I will feel

Likes

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
Guide ,
Oct 29, 2014 Oct 29, 2014

Copy link to clipboard

Copied

LATEST

For some reason I completely forgot about ListSort().  Maybe because there have been issues in the past with handling empty list items (arrays had no problems with empty items).

Anyway, what you have will work.  I'd still consider modifying your code to use a loop as it will be more flexible and maintainable.  It is not clear from your code how many pictures are actually in the list (always 2, 2 or more, etc.), so a loop will handle all the possibilities without a bunch of repeating code.

-Carl V.

Likes

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