From: Josef 'Jupp' Schugt Date: 2008-10-30T21:47:10+09:00 Subject: Re: Sorting Array Of Dates On Thu, 30 Oct 2008 02:27:32 +0100, Chandu Chandu wrote: > Hi, Everyone I am a newbie to ruby.... > > just started with some examples > > i have array > > date = ['12/09/2007','06/06/2004','10/06/2005'] > > i wanted to sort dates in ascending order i.e > ['06/06/2004','10/06/2005','12/09/2007'] you forgot to mention if the date format is reasonable (dd/mm/yyyy) or not (mm/dd/yyyy) - reasonable in the sense that the parts have an ascending or descending significance and not an arbitrary one. Anyway, here's a solution for both cases: def datecmp(a, b, reasonable = true) arra, arrb = a.split('/'), b.split('/') cmp = (arra[2] <=> arrb[2]) return cmp if (cmp = (arra[2] <=> arrb[2])) != 0 if reasonable (cmp = (arra[0] <=> arrb[0])) != 0 ? cmp : (arra[1] <=> arrb[1]) else (cmp = (arra[1] <=> arrb[1])) != 0 ? cmp : (arra[0] <=> arrb[0]) end end date = ['12/01/2007', '11/02/2007', '11/11/2005'] puts "reasonable date format" puts date.sort { |a, b| datecmp(a, b) } puts "\nno reasonable date format" puts date.sort { |a, b| datecmp(a, b, false) } that results in reasonable date format 11/11/2005 11/02/2007 12/01/2007 no reasonable date format 11/11/2005 12/01/2007 11/02/2007 but besides that the normal date format is yyyy-mm-dd. I don't know about the situation in other countries but in Germany the date format you SHOULD use (in an RFC sense, the DIN standard says so) is that ISO standard form. Other formats are only tolerable but not desireable. Which does not mean that many Germans use that format. You hear me frequently protest against forms that still use antiquated date formats :) Josef 'Jupp' Schugt -- Blog: http://penpen.goodaddress.eu/ PGP key (id 6CC6574F): http://wwwkeys.de.pgp.net/ Jabber - http://www.jabber.org/ - contact information on request