Announcement

Collapse
No announcement yet.

Need Help Sorting an Array with QuickSortArray

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Need Help Sorting an Array with QuickSortArray

    I need to sort an array, a sub category array actually. I was going to use this: http://docs.miva.com/api-functions/QuickSortArray


    so I made a test on here http://snippets.miva.com/id/1454003881/1 with my code I'll post below.

    I'm not quite there yet. I added the colorSort to take account for uppercase and lowercase getting sorted differently. I can't actually choose the 2nd parameter successfully yet on QuickSortArray.

    Does anyone have any success using QuickSortArray where you tell it what to sort by? Or should I use another function instead?

    Colin



    My code to paste into the snippet:

    Code:
    <mvt:assign name="l.settings:list[1]:name" value="'apples'" />
    <mvt:assign name="l.settings:list[1]:kind" value="'mac'" />
    <mvt:assign name="l.settings:list[1]:color" value="'a'" />
    <mvt:assign name="l.settings:list[2]:name" value="'bananas'" />
    <mvt:assign name="l.settings:list[2]:kind" value="'South American'" />
    <mvt:assign name="l.settings:list[2]:color" value="'C'" /><mvt:assign name="l.settings:list[3]" value="'Carrots'" />
    <mvt:assign name="l.settings:list[3]:name" value="'orange'" />
    <mvt:assign name="l.settings:list[3]:kind" value="'Florida'" />
    <mvt:assign name="l.settings:list[3]:color" value="'b'" />
    <mvt:assign name="l.settings:list[4]:name" value="'orange'" />
    <mvt:assign name="l.settings:list[4]:kind" value="'Michigan'" />
    <mvt:assign name="l.settings:list[4]:color" value="'d'" />
    <h2>Before</h2>
    <mvt:foreach iterator="item" array="list">
        name: &mvt:item:name;<br>
        kind: &mvt:item:kind;<br>
        color: &mvt:item:color;<br>
        colorSort: &mvt:item:colorSort;
        <hr/>
    </mvt:foreach>
    
    <h2>Add a key to sort as upper</h2>
    <mvt:foreach iterator="item" array="list">
        <mvt:assign name="l.settings:item:colorSort" value="toupper(l.settings:item:color)" />
        name: &mvt:item:name;<br>
        kind: &mvt:item:kind;<br>
        color: &mvt:item:color;<br>
        colorSort: &mvt:item:colorSort;
        <hr/>
    </mvt:foreach>
    
    <mvt:do file="g.module_library_utilities" name="l.success" value="QuickSortArray(l.settings:list, colorSort, 1)" />
    
    <h2>Do the sort and see: After</h2>
    <mvt:foreach iterator="item" array="list">
        name: &mvt:item:name;<br>
        kind: &mvt:item:kind;<br>
        color: &mvt:item:color;<br>
        colorSort: &mvt:item:colorSort;
        <hr/>
    </mvt:foreach>



    Colin
    Colin Puttick
    Miva Web Developer @ Glendale Designs

    #2
    You're pretty close. The only issue is that inserting into an array using the miva template language doesn't work quite that way. You have to create a new array and insert the new structure. The quicksortarray was slightly wrong as well. It's web voodo, but the code below works.

    I also made an online snippet as well: http://snippets.miva.com/id/1496956502/2


    TEST<br><br>
    <mvt:assign name="l.settings:list[1]:name" value="'apples'" />
    <mvt:assign name="l.settings:list[1]:kind" value="'mac'" />
    <mvt:assign name="l.settings:list[1]:color" value="'a'" />
    <mvt:assign name="l.settings:list[2]:name" value="'bananas'" />
    <mvt:assign name="l.settings:list[2]:kind" value="'South American'" />
    <mvt:assign name="l.settings:list[2]:color" value="'C'" />
    <mvt:assign name="l.settings:list[3]:name" value="'orange'" />
    <mvt:assign name="l.settings:list[3]:kind" value="'Florida'" />
    <mvt:assign name="l.settings:list[3]:color" value="'b'" />
    <mvt:assign name="l.settings:list[4]:name" value="'orange'" />
    <mvt:assign name="l.settings:list[4]:kind" value="'Michigan'" />
    <mvt:assign name="l.settings:list[4]:color" value="'d'" />

    <h2>Before</h2>
    <mvt:foreach iterator="item" array="list">
    name: &mvt:item:name;<br>
    kind: &mvt:item:kind;<br>
    color: &mvt:item:color;<br>
    <hr/>
    </mvt:foreach>

    <h3>While loop To Create New Array</h3>

    <mvt:assign name="l.elements" value="miva_array_elements(l.settings:list)" />

    <mvt:while expr="l.counter LT l.elements">
    <mvt:assign name="l.counter" value="l.counter + 1" />
    <mvt:assign name="l.struct" value="l.settings:list[l.counter]" />
    <mvt:assign name="l.struct:colorsort" value="toupper(l.struct:color)" />
    <mvt:assign name="l.success" value="miva_array_insert( l.settings:newlist, l.struct, -1 )" />
    </mvt:while>

    <mvt:comment> assing the new array to the old one </mvt:comment>

    <mvt:assign name="l.settings:list" value="l.settings:newlist" />
    <mvt:do file="g.module_library_utilities" name="l.success" value="QuickSortArray(l.settings:list,':colorsort' , 1)" />

    <h2>After Sort</h2>
    <mvt:foreach iterator="item" array="list">
    name: &mvt:item:name;<br>
    kind: &mvt:item:kind;<br>
    color: &mvt:item:color;<br>
    colorSort: &mvt:item:colorSort;
    <hr/>
    </mvt:foreach>
    M.A.D.* since 1997

    http://www.scotsscripts.com

    *miva application developers

    Comment


      #3
      Thank you Scot! I will give that a whirl today and post back.
      Colin Puttick
      Miva Web Developer @ Glendale Designs

      Comment


        #4
        If anyone wants to have their sub categories displayed alphabetically here you go.

        Edit your category page template's "Content (subcategory_listing)" section and put this code before where it starts looping the sub categories.

        Code:
        <mvt:assign name="l.elements" value="miva_array_elements(l.settings:subcats)" />
        <mvt:while expr="l.counter LT l.elements">
            <mvt:assign name="l.counter" value="l.counter + 1" />
            <mvt:assign name="l.struct" value="l.settings:subcats[l.counter]" />
            <mvt:assign name="l.struct:namesort" value="toupper(l.struct:name)" />
            <mvt:assign name="l.success" value="miva_array_insert( l.settings:newlist, l.struct, -1 )" />
        </mvt:while>
        <mvt:assign name="l.settings:subcats" value="l.settings:newlist" />
        <mvt:do file="g.module_library_utilities" name="l.success" value="QuickSortArray(l.settings:subcats,':namesort', 1)" />
        And here is some help on QuickSortArray since http://docs.miva.com/api-functions/QuickSortArray really don't explain it yet.
        The first arg is the array variable, as a variable.
        The second arg is the array subelement but you have to have single quotes around it and it needs to start with a colon :
        The last arg is the sort direction as simply a 1 or -1 where 1 is a-z and -1 is z-a
        Colin Puttick
        Miva Web Developer @ Glendale Designs

        Comment

        Working...
        X