views:

922

answers:

5

Hi!

I have a list of events and each of them has a weekday registered.

And now I'm trying to show all of these events, but ordered by the weekday name (monday, tuesday, wednesday...).

How can I do this? I'm using LinqToSQL and Lambda Expressions.

Thanks!!

+1  A: 

You would want your sort field to be an integer that corresponds to day of the week:

1 = Monday
2 = Tuesday
3 = Wednesday
...

Without more specifics, it is hard to give you a code sample. If your weekday is stored as varchar, I suggest that you create a database scalar function that takes day of week as a string argument and returns the appropriate integer.

Here is a sample in T-SQL:

create function dbo.GetDayNumber (
    @dayOfWeek varchar(9)
) returns tinyint
as begin

declare @dayNum tinyint
set @dayNum = 0

select @dayNum = 
    case 
     when @dayOfWeek = 'Monday' then 1
     when @dayOfWeek = 'Tuesday' then 2
     when @dayOfWeek = 'Wednesday' then 3
     when @dayOfWeek = 'Thursday' then 4
     when @dayOfWeek = 'Friday' then 5
     when @dayOfWeek = 'Saturday' then 6
     when @dayOfWeek = 'Sunday' then 7
    end

return @dayNum

end

Be careful - the above code returns 0 for invalid day names, which could screw with your results if your data is not perfect.

select
    MyEvent,
    DayOfWeek
from MyEventTable
order by dbo.GetDayNumber(DayOfWeek)
Terrapin
A: 

Clarification: By "each of them has a weekday registered", how is the week day represented? Date Object? Enum? String?

callingshotgun
+1  A: 
public class Event
{
  public string Day; 
}

[Test]
public void Te()
{
  var dayIndex = new List<string> {"MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY", "SUNDAY"};
  var list = new List<Event> {new Event(){Day = "saturday"}, new Event() {Day = "Monday"}, new Event() {Day = "Tuesday"}};
  var sorted = list.OrderBy(e => dayIndex.IndexOf(e.Day.ToUpper()));
  foreach (var e in sorted)
  {
    Console.WriteLine(e.Day);
  }
}
RossFabricant
rossfabricant, I did something like you've told above and it worked just great by creating this List<string> of the weekday names! Thanks!
AndreMiranda
+2  A: 

I know it is possible to sort a list using a custom class which implements IComparer(Of DateTime). So you'd create a class like this:

Public Class WeekDayComparer
  Implements IComparer(Of MyEvent)

  Public Function Compare(ByVal x As MyEvent, ByVal y As MyEvent) As Integer Implements System.Collections.Generic.IComparer(Of MyEvent).Compare

    Return GetDayOfWeekNumber(x.DayOfWeek).CompareTo(GetDayOfWeekNumber(y.DayOfWeek))

  End Function

  Private Function GetDayOfWeekNumber(ByVal dayOfWeek As String) As Integer

     Select Case dayOfWeek.ToLower()
       Case "monday"
         Return 0
       Case "tuesday"
         Return 1
       Case "wednesday"
         Return 2
       Case "thursday"
         Return 3
       Case "friday"
         Return 4
       Case "saturday"
         Return 5
       Case "sunday"
         Return 6
       Case Else
         Return 7
     End Select

  End Function

End Class

Then you'd take your list of objects and sort them using a your WeekDayComparer class.

events.Sort(New WeekDayComparer())

This is VB, but easily converts to C#.

Brandon Montgomery
own IComparer is the best way, I think
abatishchev
He doesn't have a DateTime, he's storing the value in the DB as a varchar
Joseph
+1  A: 

Assuming your Event object contains a DateTime "date", you can just sort by the DateTime.DayOfWeek enum, like so:

List<Event> events = new List<Event>(); // filled elsewhere
events.Sort((x,y) => x.Date.DayOfWeek.CompareTo(y.Date.DayOfWeek));

That'll sort the events Sunday-Saturday, and since you're only doing weekdays, the Sunday and Saturday shouldn't matter.

Chris Doggett