473,849 Members | 1,992 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 13313
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 DateTimeCompare r : IComparer<DateT ime>
{
private bool _sortDescending = false;

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

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

//sort ascending
MyArrayList.Sor t(new DateTimeCompare r(false));

or

//sort descending
MyArrayList.Sor t(new DateTimeCompare r(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
1954
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 to the database. How can accomplish this task? Thanks!
1
2699
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, paginantion or accepting the add and remove item events. What i am observing is that none of the vents are happening... (Sort, page, or item). I am sure I am missing something basic here... Need help... Thanks much
3
6466
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. Simple code: Dim y As New ArrayList y.Add("Abc") y.Add("abc") y.Add("_bc")
19
2471
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 would like to sort the arraylist based on object.name - is that possible? Thanks! Derek
0
1679
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 investigate any further but there was nothing about it when googling the newsgroups so I am posting a note in case it helps somebody else out. The problem was in a system with a web front end calling through to a chain of 3 webservices i.e.
48
4501
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 lot faster with both methods using .NET 1.1, that's why I am pretty sure its a (rather serious) bug. Below you can find C# test case that should allow you to reproduce this error, to run it you will need to put 2 data files into current directory...
3
2387
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 represents my menu system, and I would like to sort the third level menu items only and not the first two. So, I've been trying to use some of the suggestions I've found here about sorting. Mainly, I've created a comparison interface.
4
4970
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 listed first followed by the files.
3
3129
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 unfortunately in opposite to the example from this link here it does not sort. I Create Nodes ( color, name ) and add them to nodeList in Graph class. Then i try to sort.. The code is relatively simple:
0
9895
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9741
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11008
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9501
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7899
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7070
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4545
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4140
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3177
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.