Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • bev0 39 posts 59 karma points
    Aug 30, 2012 @ 15:39
    bev0
    0

    sort a list from an order of a different list

    i have a sort order list of movies (order from left to right)

    <movies>movieF,movieC,movieG</movies>
    

    now i want to take that sort order list and sort on top of this huge movies list of mine

    <moviesList>
     <movie>movieA</movie>
     <movie>movieB</movie>
     <movie>movieC</movie>
     <movie>movieD</movie>
     <movie>movieE</movie>
     <movie>movieF</movie>
     <movie>movieG</movie>
     <movie>movieH</movie>
    </movieList>
    

    result i want:

    <moviesList>
     <movie>movieF</movie>
     <movie>movieC</movie>
     <movie>movieG</movie>
     <movie>movieA</movie>
     <movie>movieB</movie>
     <movie>movieD</movie>
     <movie>movieE</movie>
     <movie>movieH</movie>
    </movieList>
    

    would someone please give me some guidance of how to achieve such thing. i've tried to create a variable $sortlist, and then add delimited character around and then use substring-before trick on the sort. result is my sorted list did show up on top before the rest of the movies but it's not on the right order. please help.

  • Fuji Kusaka 2203 posts 4220 karma points
    Aug 30, 2012 @ 16:30
    Fuji Kusaka
    0

    Hi,

    Have a look at this threat where Chriztian AKA Mr XSLT explains how to achieve this

    Hope this will help you

  • bev0 39 posts 59 karma points
    Aug 30, 2012 @ 19:02
    bev0
    0

    i dont see nor understand how using translate() will help my situation, would you mind explain ?

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 7x admin c-trib
    Aug 30, 2012 @ 22:11
    Chriztian Steinmeier
    0

    Hi bev0,

    Yeah - you need to pull a different trick out of the bag for this to work... something like this:

    <!-- Grab the movies - change to suit your data -->
    <xsl:variable name="movies" select="$currentPage/movieList/movie" />
    
    <!-- $movieSort is the <movies>xxx,yyy,zzz</movies> from somewhere -->
    <xsl:variable name="promoted" select="umbraco.library:Split($movieSort/movies, ',')//value" />
    
    <xsl:template match="/">
        <xsl:apply-templates select="$movies">
            <!-- First sort puts "promoted" elements at the top -->
            <xsl:sort select="number(boolean($promoted[. = current()]))" data-type="number" order="descending" />
            <!-- Second sort puts them in the correct order by  -->
            <xsl:sort select="count($promoted[. = current()] | $promoted[. = current()]/preceding-sibling::*)" data-type="number" order="ascending" />
        </xsl:apply-templates>
    </xsl:template>
    
    <!-- Sample output template for each movie -->
    <xsl:template match="movie">
        <p><xsl:value-of select="." /></p>
    </xsl:template>
    

    /Chriztian

  • bev0 39 posts 59 karma points
    Aug 31, 2012 @ 00:39
    bev0
    0

    Hi Chriztian,

    Thank you for your quick reply. Would you mind revise your example in different form by using <xsl:for-each> and <xsl:sort> ? The big movie list is current stuck in a massive <xsl:sort> order where I'm not sure if it's a good idea to break it down into small apply-template methods. Sorry if it's a newbie request. Also, if you had any reasons why you chose to normalize the whole deal in several <xsl:template>, please explain, i'd love to hear/learn from the expert :)   Once again, thanks!

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 7x admin c-trib
    Sep 01, 2012 @ 16:33
    Chriztian Steinmeier
    0

    Hi bev0,

    OK - here's how the two methods relate:

    Using for-each:

    <xsl:for-each select="A">
        <xsl:sort select="B" />
        <xsl:sort select="C" />
    
        <!-- Do something --> 
    
    </xsl:for-each>

    Using apply-templates:

    <xsl:apply-templates select="A">
        <xsl:sort select="B" />
        <xsl:sort select="C" />
    </xsl:apply-templates>
    
    <xsl:template match="A">
        <!-- Do something -->
    </xsl:template>
    
    So, in a simple scenario you may like the for-each better, but the power of apply-templates comes in as soon as the "A" doesn't relate to only one piece of information, but many different.
    Adding another type (e.g. a TVSeries or ThreeDMovie) to the collection which should use the same sorting logic, but render in a slightly different way, is as simple as making sure it's selected in the apply-templates' select attribute and then adding specific templates for them.
    In a for-each construct, you'd have to use a set of choose/when or if statements to do the different renderings...

    /Chriztian

  • bev0 39 posts 59 karma points
    Sep 04, 2012 @ 17:32
    bev0
    0

    thank you for your detail explanation, Chriztian. i learnt a thing or two after reading your comments :)

Please Sign in or register to post replies

Write your reply to:

Draft