SortTool

steelej's Avatar

steelej

03 Mar, 2015 06:54 PM

I'm having some difficulty with the sortTool and can't figure out why. I'm indexing pages that contain student employment job listings. I'm pulling out the path to the page, the job title and the wage. The wage is entered in this format: 00.00, so for example it would be 13.00 or 8.50. I'm trying to sort by the wage so that it lists the jobs from the highest to the lowest wage on the page. Secondarily, I want to list the jobs with the same wage, alphabetically (for instance, all the jobs that pay 8.00 would be listed together in alphabetical order by job title.

Hasn't worked for me yet.

  1. 1 Posted by Ryan Griffith on 04 Mar, 2015 02:46 PM

    Ryan Griffith's Avatar

    Hi Jim,

    I believe you are going to run into an issue by using two sort criterion. Specifically, it's going to sort the same list twice, so you won't get that grouping by wage you are looking for. XSLT might be a better choice there using <xsl:key>.

    That being said, you could sort by job title and then loop over the jobs and generate an array of Map objects. You can then use the Sort Tool to sort by a wage property. You would then loop over the resulting list. When you have a moment, give the following a try for starters and let me know how it works out:

    ## This chooses only folders that have jobs
    #set ( $assets = $_XPathTool.selectNodes($contentRoot, "/system-index-block/system-folder/system-page[name!='index'][system-data-structure/open ='Yes']") )
    #set ( $lookup = [] )
    
    #if ($assets.size() > 0)
        ## Sort jobs by title
        $_SortTool.addSortCriterion("system-data-structure/job", "", "text", "ascending", "upper-first")
        $_SortTool.sort($assets)
        
        ## Populate the lookup table that can be sorted on.
        #foreach ( $asset in $assets )
            #set ( $wage = 0 )
            ## Make sure there is a wage; otherwise, it will default to 0.
            #if (!$_PropertyTool.isNull($asset.getChild("system-data-structure").getChild("wage").value))
                ## Sorting by Double doesn't seem to work, so we're essentially multiplying by 100 by removing the period and converting to an integer.
                #set ( $wage = $_MathTool.toInteger($asset.getChild("system-data-structure").getChild("wage").value.trim().replace(".", "")) )
            #end
            #set ( $_void = $lookup.add({
                "page": $asset,
                "wage": $wage
            }) )
        #end
        
        <h6>Jobs by Title</h6>
        <ul class="columnnav">
    
        ## Loop through the lookup table that is sorted by wage.
        #foreach ( $job in $_SortTool.sort($lookup, ["wage"]) )
            #set ( $asset = $job.get("page") )
            #set ( $path = $asset.getChild("path").value )
            #set ( $jobtitle = $asset.getChild("system-data-structure").getChild("job").value )
            #set ( $pay = $_MathTool.div($job.get("wage"), 100) )
            
            #set ( $curr = "" )
            #if ( $asset.hasAttribute("current") )
                #set ( $curr = "current" )
            #end
            
            #if ( $pay > 0 )
            <li>
                <a href="${path}" class="${curr}">$_EscapeTool.xml($jobtitle)</a> -- $${pay}/hr
            </li>
            #end
        #end
        </ul>
    #end
    

    Please let me know if you have any questions.

    Thanks!

  2. 2 Posted by steelej on 04 Mar, 2015 03:09 PM

    steelej's Avatar

    It appears to work very well. That's admittedly not one I would have figured out by myself. Thanks

  3. steelej closed this discussion on 04 Mar, 2015 03:09 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