Sort List of Strings

ces55739's Avatar

ces55739

09 Apr, 2015 08:36 PM

Hi,

I am wondering if there is a nice way to sort a list of strings. I want to transform $my_list = ["c", "d", "a", "b"] => ["a", "b", "c", "d"].

I have looked into the Sort Tool, but that seems to only deal with nodes. Is there a nice way to simply do this?

Thanks!

  1. 1 Posted by Wing Ming Chan on 10 Apr, 2015 12:23 AM

    Wing Ming Chan's Avatar

    Hi,

    If you are willing to turn the list into a map, it is possible:

    #set( $items= [] )
    #set( $tmp = $items.add( { "value" : "c" } ) )
    #set( $tmp = $items.add( { "value" : "d" } ) )
    #set( $tmp = $items.add( { "value" : "a" } ) )
    #set( $tmp = $items.add( { "value" : "b" } ) )
    
    
    <ul> #foreach ( $item in $_SortTool.sort( $items, [ "value:asc" ] ) ) <li>$item.value</li> #end </ul>
    Wing
  2. 2 Posted by Nadira Yasmeen on 10 Apr, 2015 01:23 AM

    Nadira Yasmeen's Avatar

    Another way to sort strings is by converting the ArrayList to an Array--then passing that as a sort parameter, for example:

    #set($myList = ["c","d","a","b"])
    #set($sorted = $_SortTool.sort($myList.toArray()))
    
    #foreach($obj in $sorted )
         $obj
    #end
    
  3. 3 Posted by ces55739 on 13 Apr, 2015 02:55 PM

    ces55739's Avatar

    Thanks for the suggestions Wing and Nadira!!

    I will be using both of these in the future.

  4. ces55739 closed this discussion on 13 Apr, 2015 02:55 PM.

Discussions are closed to public comments.
If you need help with Cascade CMS please start a new discussion.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac

 

26 Aug, 2016 01:19 PM
25 Aug, 2016 03:02 PM
25 Aug, 2016 12:50 PM
24 Aug, 2016 08:43 PM
24 Aug, 2016 07:20 PM
21 Aug, 2016 01:20 PM