Index: Components/ArticleTable/ArticleTablePlugin.m
===================================================================
--- Components/ArticleTable/ArticleTablePlugin.m	(revision 3886)
+++ Components/ArticleTable/ArticleTablePlugin.m	(working copy)
@@ -29,27 +29,56 @@
 #import "GNUstep.h"
 #endif
 
-int compareArticleHeadlines( id articleA, id articleB, void* context ) {
+#define ARTICLETABLE_SORT_MODE @"ARTICLETABLE_SORT_MODE"
+enum SORT_MODE
+{
+   BY_DATE_ASCENDING,
+   BY_DATE_DESCENDING,
+   BY_HEADLINE_ASCENDING,
+   BY_HEADLINE_DESCENDING,
+   BY_RATING_ASCENDING,
+   BY_RATING_DESCENDING,
+};
+
+NSComparisonResult compareArticleHeadlines( id articleA, id articleB, void* context ) {
     id<Article> a = (id<Article>) articleA;
     id<Article> b = (id<Article>) articleB;
+    int sort_mode = *(int *)context;
     
+    if (sort_mode == BY_HEADLINE_DESCENDING)
+      {
+	b = (id<Article>) articleA;
+	a = (id<Article>) articleB;
+      }
     return [[a headline] caseInsensitiveCompare: [b headline]];
 }
 
-int compareArticleDates( id articleA, id articleB, void* context ) {
+NSComparisonResult compareArticleDates( id articleA, id articleB, void* context ) {
     id<Article> a = (id<Article>) articleA;
     id<Article> b = (id<Article>) articleB;
+    int sort_mode = *(int *)context;
     
+    if (sort_mode == BY_DATE_DESCENDING)
+      {
+	b = (id<Article>) articleA;
+	a = (id<Article>) articleB;
+      }
     return [[a date] compare: [b date]];
 }
 
-int compareArticleRatings( id articleA, id articleB, void* context) {
+NSComparisonResult compareArticleRatings( id articleA, id articleB, void* context) {
     id<Article> a = (id<Article>) articleA;
     id<Article> b = (id<Article>) articleB;
+    int sort_mode = *(int *)context;
     
     int ratingA = [a rating];
     int ratingB = [b rating];
     
+    if (sort_mode == BY_RATING_DESCENDING)
+      {
+	ratingB = [a rating];
+	ratingA = [b rating];
+      }
     if (ratingA == ratingB) {
         return NSOrderedSame;
     } else if (ratingA > ratingB) {
@@ -159,8 +188,36 @@
 
 -(void)componentDidUpdateSet: (NSNotification*) aNotification
 {
+  NSUserDefaults *defaults;
+  int sort_mode;
+  NSArray *newArray;
+
+  defaults = [NSUserDefaults standardUserDefaults];
+
+  if ([defaults objectForKey: ARTICLETABLE_SORT_MODE])
+    {
+      sort_mode = [defaults integerForKey: ARTICLETABLE_SORT_MODE];
+    }
+  else
+    {
+      sort_mode = BY_DATE_ASCENDING;
+    }
+
     // Update articles
-    ASSIGN(articles, [[[aNotification object] objectsForPipeType: [PipeType articleType]] allObjects]);
+  newArray = [[[aNotification object] objectsForPipeType: [PipeType articleType]] allObjects];
+  if (sort_mode < BY_HEADLINE_ASCENDING)
+    {
+      newArray = [newArray sortedArrayUsingFunction: compareArticleDates context: (void *)&sort_mode];
+    }
+  else if (sort_mode < BY_RATING_ASCENDING)
+    {
+      newArray = [newArray sortedArrayUsingFunction: compareArticleHeadlines context: (void *)&sort_mode];
+    }
+  else
+    {
+      newArray = [newArray sortedArrayUsingFunction: compareArticleRatings context: (void *)&sort_mode];
+    }
+  ASSIGN(articles, newArray);
     
     // Reload table contents
     [table reloadData];
@@ -229,12 +286,65 @@
   mouseDownInHeaderOfTableColumn: (NSTableColumn*) aTableColumn
 {
     NSArray* newArray = nil;
+    NSUserDefaults *defaults;
+    int sort_mode;
+
+    defaults = [NSUserDefaults standardUserDefaults];
+
+    if ([defaults objectForKey: ARTICLETABLE_SORT_MODE])
+      {
+	sort_mode = [defaults integerForKey: ARTICLETABLE_SORT_MODE];
+      }
+    else
+      {
+	sort_mode = BY_DATE_ASCENDING;
+      }
+
     if (aTableColumn == headlineCol) {
-        newArray = [articles sortedArrayUsingFunction: compareArticleHeadlines context: nil];
+      if (sort_mode == BY_HEADLINE_DESCENDING)
+	{
+	  sort_mode = BY_HEADLINE_ASCENDING;
+	}
+      else if (sort_mode == BY_HEADLINE_ASCENDING)
+	{
+	  sort_mode = BY_HEADLINE_DESCENDING;
+	}
+      else
+	{
+	  sort_mode = BY_HEADLINE_ASCENDING;
+	}
+
+      newArray = [articles sortedArrayUsingFunction: compareArticleHeadlines context: (void *)&sort_mode];
     } else if (aTableColumn == dateCol) {
-        newArray = [articles sortedArrayUsingFunction: compareArticleDates context: nil];        
+      if (sort_mode == BY_DATE_DESCENDING)
+	{
+	  sort_mode = BY_DATE_ASCENDING;
+	}
+      else if (sort_mode == BY_DATE_ASCENDING)
+	{
+	  sort_mode = BY_DATE_DESCENDING;
+	}
+      else
+	{
+	  sort_mode = BY_DATE_ASCENDING;
+	}
+
+      newArray = [articles sortedArrayUsingFunction: compareArticleDates context: (void *)&sort_mode];
     } else if (aTableColumn == ratingCol) {
-        newArray = [articles sortedArrayUsingFunction: compareArticleRatings context: nil];
+      if (sort_mode == BY_RATING_DESCENDING)
+	{
+	  sort_mode = BY_RATING_ASCENDING;
+	}
+      else if (sort_mode == BY_RATING_ASCENDING)
+	{
+	  sort_mode = BY_RATING_DESCENDING;
+	}
+      else
+	{
+	  sort_mode = BY_RATING_ASCENDING;
+	}
+
+      newArray = [articles sortedArrayUsingFunction: compareArticleRatings context: (void *)&sort_mode];
     } else {
         [NSException raise: @"BadColumnException"
                     format: @"Unknown column %@", [aTableColumn identifier]];
@@ -242,6 +352,7 @@
     
     [self setNewArrayWithoutNotification: newArray];
     [self notifyChanges];
+    [defaults setInteger: sort_mode forKey: ARTICLETABLE_SORT_MODE];
 }
 
 -(void) tableView: (NSTableView*) aTableView
