Link to home
Start Free TrialLog in
Avatar of GAUTAM
GAUTAMFlag for United States of America

asked on

Sorting Table Rows By Dates in Javascript

Hi Experts,

I have a requirement of sorting the rows of a html table.

Here the rows  of the table are copied into an actual array to maintain a copy.
The original html table consists of the foll rows saved in the index 0,1,2,3
09/09/2012 Gaara
09/08/2012 Vegeta
09/07/2012 Nappa
09/06/2012 Goku


Then i parse only the date value from the row which is in mm/dd/yyyy and also i get the actual row number in which the date occurs which is in the form 0,1,2,3,.. and so on.
The format is as follows:
09/09/2012~0
09/08/2012~1
09/07/2012~2
09/06/2012~4

What i need is that is the actual sorted row number like which in the above case is 3,2,1,0 so that i can arrange the rows using the actual copy saved before.

If i get this order then i can arrange the original data as follows
09/06/2012 Goku
09/07/2012 Nappa
09/08/2012 Vegeta
09/09/2012 Gaara

Please help...
Avatar of Lee Wadwell
Lee Wadwell
Flag of Australia image

Are you wanting the table to be sortable or to be displayed in a fixed order?

If a fixed order ... how is the table built?  Is it sourced from a database via a query or what?

If you want the table sortable ... there are various packages available than can do that.
How are you storing your sorted values you give the example

09/09/2012~0

This this mean you have an array of dates where the ~0 indicates the index of the array?

And what you want is after you have sorted the array how to get the original index of the source row?

How are you sorting the array of dates - can you not make that an array of objects or arrays with each element having two values - date and original row number?
Avatar of GAUTAM

ASKER

@lwadwell & @julianH:Thanks for replying.

@lwadwell:After the table is displayed after queryring the db I want the ros of the table to be flexible such that the rows of the table should be sortable by different columns of the table in this case date.

@julianH:09/09/2012~0
This represents the date vale and the row index which was stored earlier for repopulating after sorting after we get the sort order.
While comparision I do not consider ~0 part by taking the value using substring function and later arrange the ascening or descending aray by using the part after ~ symbol.

BTW how do we do the same comparision of times in the format hh:mm:ss and how is it different from the date comparison.
ASKER CERTIFIED SOLUTION
Avatar of Lee Wadwell
Lee Wadwell
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@julianH:09/09/2012~0
This represents the date vale and the row index which was stored earlier for repopulating after sorting after we get the sort order.

If it is part of the string then why not just chop of the last bit of the string to get the original index?

Not seeing the problem here.

And as lwadwell says - there are many sorting tools out there. Personally I prefer to sort server side than send hundreds of records to the client - but that's a personal call.