Click here to Skip to main content
15,884,099 members
Home / Discussions / C#
   

C#

 
QuestionProcess start problems [modified] Pin
Xmen Real 30-May-08 6:44
professional Xmen Real 30-May-08 6:44 
AnswerRe: Process start problems Pin
Ennis Ray Lynch, Jr.30-May-08 7:57
Ennis Ray Lynch, Jr.30-May-08 7:57 
GeneralRe: Process start problems Pin
Xmen Real 30-May-08 8:00
professional Xmen Real 30-May-08 8:00 
QuestionPassing info from a thread Pin
brsecu30-May-08 5:21
brsecu30-May-08 5:21 
AnswerRe: Passing info from a thread Pin
Judah Gabriel Himango30-May-08 5:27
sponsorJudah Gabriel Himango30-May-08 5:27 
GeneralRe: Passing info from a thread Pin
Ennis Ray Lynch, Jr.30-May-08 6:04
Ennis Ray Lynch, Jr.30-May-08 6:04 
GeneralRe: Passing info from a thread Pin
Judah Gabriel Himango30-May-08 7:39
sponsorJudah Gabriel Himango30-May-08 7:39 
QuestionSortable List<t></t> Pin
eggsovereasy30-May-08 4:18
eggsovereasy30-May-08 4:18 
I've being using objectDataSources to populate some data controls lately. My data access objects return collections of objects rather than DataTables or DataSets. The inherent problem with this is you can't sort a collection by any column (e.g. if I had an EmployeeCollection filled with Employee objects I couldn't sort by Employee.LastName in one method and then Employee.Id in another). So I set out to create a sortable list I could inherit from and be able to sort my collections.

What I have is below, but the problem I have is that I'm converting my generic variables to strings so that I can compare them. I suppose this works fine as long as I override ToString() on my objects so that it makes sense, but it seems like a bit of a kludge. Does anyone have any other ideas that might work?

using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;

namespace IssueTracker.Business.Collections
{
	public class SortableList<T> : List<T>
	{
		public void SortBy(string sortExpression, SortDirection sortDirection)
		{
			Sort(delegate(T t1, T t2) { return Compare(t1, t2, sortExpression, sortDirection); });
		}

		private static int Compare(T t1, T t2, string propertyName, SortDirection sortDirection)
		{
			// get property values values
			object t1Value = t1.GetType().GetProperty(propertyName).GetValue(t1, null);
			object t2Value = t2.GetType().GetProperty(propertyName).GetValue(t2, null);

			// compare values
			StringComparer comparer = StringComparer.CurrentCulture;
			int result = comparer.Compare(t1Value.ToString(), t2Value.ToString());

			// reverse result if it's descending
			if (sortDirection == SortDirection.Descending)
				result = result*-1;

			return result;
		}
	}
}

AnswerRe: Sortable List Pin
led mike30-May-08 4:46
led mike30-May-08 4:46 
QuestionImplement a Cyber Coffe in C# Pin
hendryck30-May-08 4:09
hendryck30-May-08 4:09 
AnswerRe: Implement a Cyber Coffe in C# Pin
Colin Angus Mackay30-May-08 4:22
Colin Angus Mackay30-May-08 4:22 
QuestionCoverflow/Shift window switching effect Pin
bDreea30-May-08 4:03
bDreea30-May-08 4:03 
QuestionHELP HELP URGENTLY!!!! Pin
Wajeeha30-May-08 2:55
Wajeeha30-May-08 2:55 
AnswerRe: HELP HELP URGENTLY!!!! Pin
Wajeeha30-May-08 3:09
Wajeeha30-May-08 3:09 
GeneralRe: HELP HELP URGENTLY!!!! Pin
subhankarbiswas30-May-08 9:00
subhankarbiswas30-May-08 9:00 
AnswerRe: HELP HELP URGENTLY!!!! PinPopular
Andy *M*30-May-08 3:17
Andy *M*30-May-08 3:17 
AnswerRe: HELP HELP URGENTLY!!!! PinPopular
Colin Angus Mackay30-May-08 3:25
Colin Angus Mackay30-May-08 3:25 
AnswerRe: HELP HELP URGENTLY!!!! PinPopular
Baconbutty30-May-08 3:37
Baconbutty30-May-08 3:37 
GeneralRe: HELP HELP URGENTLY!!!! Pin
eggsovereasy30-May-08 4:04
eggsovereasy30-May-08 4:04 
RantRe: HELP HELP URGENTLY!!!! Pin
blakey40430-May-08 4:27
blakey40430-May-08 4:27 
RantRe: HELP HELP URGENTLY!!!! Pin
blakey40430-May-08 4:27
blakey40430-May-08 4:27 
AnswerRe: HELP HELP URGENTLY!!!! Pin
Bert delaVega30-May-08 12:47
Bert delaVega30-May-08 12:47 
QuestionWorking with Decimals Pin
Jim Warburton30-May-08 2:47
Jim Warburton30-May-08 2:47 
AnswerRe: Working with Decimals Pin
Matthew Butler30-May-08 3:34
Matthew Butler30-May-08 3:34 
AnswerRe: Working with Decimals Pin
CPallini30-May-08 3:39
mveCPallini30-May-08 3:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.