473,414 Members | 1,848 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Sort Arraylist

I have an arraylist that contains datetime values. What is the best way
to sort this arraylist by date ascending?

Thanks,

Matt
Jun 5 '06 #1
4 13247
M Harvey wrote:
I have an arraylist that contains datetime values. What is the best way
to sort this arraylist by date ascending?


ArrayList has a Sort method which should sort them correctly. If you
need to sort them in a descending order or if there is more data than
just the DateTime, you will have to create a class that implements
IComparer. That class would have a single Compare method which you
would implement to compare 2 of your specific objects.

Here is a simple implementation that uses DateTime objects. It uses
the generic IComparer class so it requires v2.0 of the framework for
v1.1, you would code it the same way except using the IComparer class:

class DateTimeComparer : IComparer<DateTime>
{
private bool _sortDescending = false;

//pass true to the constructor to sort descending
public DateTimeComparer(bool descending)
{
_sortDescending = descending;
}

public int Compare(DateTime x, DateTime y)
{
if (_sortDescending)
return y.CompareTo(x);
else
return x.CompareTo(y);
}
}
And you would call the code like this:

//sort ascending
MyArrayList.Sort(new DateTimeComparer(false));

or

//sort descending
MyArrayList.Sort(new DateTimeComparer(true));
NOTE You don't need this class if your arraylist only contains
DateTime instances because DateTime implements IComparable and the the
ArrayList.Sort method will be able to sort them directly. I just
showed this class to show how easy the IComparer interface is.

Jun 5 '06 #2
M Harvey wrote:
I have an arraylist that contains datetime values. What is the best way
to sort this arraylist by date ascending?


ArrayList has a Sort method, just call it.
--
Tom Porterfield
Jun 5 '06 #3
Tom Porterfield wrote:
M Harvey wrote:
I have an arraylist that contains datetime values. What is the best way
to sort this arraylist by date ascending?

ArrayList has a Sort method, just call it.

I've tried it...it doesn't seem to be working right, or perhaps I don't
have a complete understanding of how it's supposed to be used.
Jun 5 '06 #4
M Harvey wrote:
I have an arraylist that contains datetime values. What is the best way
to sort this arraylist by date ascending?


ArrayList has a Sort method, just call it.


I've tried it...it doesn't seem to be working right, or perhaps I don't
have a complete understanding of how it's supposed to be used.


Can you post more details on what is not working right about the Sort
method?
--
Tom Porterfield

Jun 5 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Leon | last post by:
I have six textbox controls on my webform that allows the user to enter any numbers from 1 to 25 in any order. However, I would like to sort those numbers from least to greatest before sending them...
1
by: Gunjan Garg | last post by:
Hello All, I am working to create a generic datagrid which accepts a datasource(ListData - This is our own datatype) and depending on the calling program customizes itself for sorting,...
3
by: Adam J. Schaff | last post by:
Hello. I recently noticed that the Sort method of the .NET ArrayList class does not behave as I expected. I expect 'A' < '_' < 'a' (as per their ascii values) but what I got was the opposite....
19
by: Derek Martin | last post by:
Hi there, I have been playing with sorting my arraylist and having some troubles. Maybe just going about it wrong. My arraylist contains objects and one of the members of the object is 'name.' I...
0
by: Grant Wickman | last post by:
Our team has just fixed a really nasty problem that appears to be caused by an obscure bug in the sort method of Arraylist and daisy-chained webservice stubs. We've fixed the bug so I'll not...
48
by: Alex Chudnovsky | last post by:
I have come across with what appears to be a significant performance bug in ..NET 2.0 ArrayList.Sort method when compared with Array.Sort on the same data. Same data on the same CPU gets sorted a...
3
by: jtfaulk | last post by:
Re: ArrayList, Sort, Menu, IComparer, Object, multidemensional I have a multi-dimensional arraylist, and I would like to sort one level of it but not all. The multi-dimensional arraylist...
4
by: rn5a | last post by:
Can the items in a ListBox be sorted by the name of the items? The ListBox actually lists all directories & files existing in a directory on the server. Note that all the directories should be...
3
by: djp | last post by:
Hi I have to sort arraylist. I tried to do this using this page as a reference: http://www.java2s.com/Code/CSharp/Collections-Data-Structure/UseIComparer.htm I did it exactly the same way but...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.