SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Member
    Join Date
    Jul 2006
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Sorting the table contents using Javascript

    Hi all,

    I am working on a application similar to the Adobe's Flex store. Everything is working as intended. The main stumbling block is with the sorting of the products by brand name, by price etc. I am using the concept of table sort. But with this i am able to sort the rows only (not all the products are getting sorted), i.e it is comparing first product of each row and carrying out the sort operation. I need all the products to be sorted depending on the sort criteria.

    One concept i am trying to go with is the populating an temporary array which stores the products, and i am trying to sort this array and display the contents of the sorted array. I am not getting this to work.

    Please refer me some code/links so that ithey can be helpful.

    Suggestions are encouraged,


    Thanks in advance,
    Tannu

  2. #2
    Take The Blue Pill neilmerton's Avatar
    Join Date
    Jul 2004
    Location
    Leicester, UK
    Posts
    871
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  3. #3
    SitePoint Member
    Join Date
    Jul 2006
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    these scripts sort a table by rows (i.e they compare two successive rows).
    My aim is to sort randomly arranged products (say in a table of 4X4) according to their price, or some other criteria. (Reference Adobe's flex store, where in u can select a criteria to sort the products currently displayed.)

    Thanks,
    Tannu

  4. #4
    SitePoint Addict darkwater23's Avatar
    Join Date
    Nov 2005
    Location
    Omaha, NE
    Posts
    335
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Couldn't you dynamically create a SQL call to sort the data for you?

  5. #5
    SitePoint Member
    Join Date
    Jul 2006
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am using XML to store product information and javascript to access the XML file using XMLHtttpRequest object.

    I am not familiar with SQL.

  6. #6
    SitePoint Addict darkwater23's Avatar
    Join Date
    Nov 2005
    Location
    Omaha, NE
    Posts
    335
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, I'm assuming that you have put the product info into an array or a multi-dimensional array. Depending on the criteria, you're going to have to bubble sort the array by comparing row against each other, moving data around and then loop over the array enough times to sort everything. Does that make sense?

  7. #7
    SitePoint Member
    Join Date
    Jul 2006
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is there any other way to go with? If any, please suggest.

  8. #8
    SitePoint Addict darkwater23's Avatar
    Join Date
    Nov 2005
    Location
    Omaha, NE
    Posts
    335
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm afraid not. Since you don't have a database to make SQL calls against, you're going to have to sort the data programatically.

    http://en.wikipedia.org/wiki/Bubble_sort

    Otherwise, you could staticly code pre-sorted data and change sources depending on user selection.

  9. #9
    SitePoint Aficionado JVLB's Avatar
    Join Date
    Jan 2002
    Location
    N 44 56.537' W 123 3.683'
    Posts
    1,127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    JavaScript arrays have a sort() method which can use a user-defined function to derive the sort values. I'm not really clear on your situation, but if you are using XML it should be possible to directly populate an array with the objects in question and sort, then use the DOM to display the result.


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
  •