SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Evangelist
    Join Date
    Apr 2004
    Location
    Boston
    Posts
    482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    sorting table rows according to one value

    Hello,

    Is there an easy way to sort all rows associated with a query by the value of one of the rows? Sort of like ranking.

    for example:

    PHP Code:
    <?php 
    $f
    [1] = 'text 1';
    $f[2] = 'text 2';
    $f[3] = 'text 3';

    $val[1] = '6';
    $val[2] = '1';
    $val[3] = '25';

    ?>
    I want to display both $f and $val sorted by $val.

    Thanks.

  2. #2
    SitePoint Enthusiast
    Join Date
    Jul 2009
    Location
    Austria
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A MySQL-query?
    => SELECT ... ORDER BY col
    You can order ASC or DESC
    See mysql-homepage order-by-optimization

  3. #3
    SitePoint Addict
    Join Date
    Oct 2008
    Posts
    295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Have you tried asort() or arsort() ? This if you sort in php.. else what blubb said.

  4. #4
    SitePoint Evangelist
    Join Date
    Apr 2004
    Location
    Boston
    Posts
    482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by blubb View Post
    A MySQL-query?
    => SELECT ... ORDER BY col
    You can order by ASC or DESC
    See mysql-Homepage order-by-optimization
    Sorry, i left out the most important part. the value is actually an average of a bunch of results.

    The first query looks for distinct results and then the second queries averages for each of those results. I just need to order the averages of all results.

  5. #5
    SitePoint Enthusiast
    Join Date
    Jun 2008
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    so, you want to display "text 1 appeared 6 times" for example?

  6. #6
    SitePoint Evangelist
    Join Date
    Apr 2004
    Location
    Boston
    Posts
    482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ardentmirage View Post
    so, you want to display "text 1 appeared 6 times" for example?
    no, i want it to force rank by $val. so i have some text description and a numerical score, i want it to score in order, the problem is the score isnt necessarly raw data it's produced by a math problem after a query.

  7. #7
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    see usort()


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •