Index: views_audio.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/audio/views_audio.inc,v
retrieving revision 1.10
diff -u -p -u -r1.10 views_audio.inc
--- views_audio.inc	31 Jul 2007 23:41:22 -0000	1.10
+++ views_audio.inc	29 Aug 2007 19:21:59 -0000
@@ -80,6 +80,8 @@ function audio_views_tables() {
     ),
   );
 
+  $numeric_tags = array('track', 'year');
+
   foreach (audio_get_tags_allowed() as $tag) {
     $tables['audio_metadata_'. $tag] = array(
       'name' => 'audio_metadata',
@@ -121,6 +123,14 @@ function audio_views_tables() {
         ),
       ),
     );
+
+    // Use different handlers for numeric tags.
+    if (in_array($tag, $numeric_tags)) {
+      $tables["audio_metadata_$tag"]['sorts']['value']['handler'] = 'audio_views_sort_handler_numeric_tag';
+      // Set notafield to TRUE so that our handler can add the field.
+      $tables["audio_metadata_$tag"]['fields']['value']['notafield'] = TRUE;
+      $tables["audio_metadata_$tag"]['fields']['value']['query_handler'] = 'audio_views_field_query_handler_numeric';
+    }
   }
 
   return $tables;
@@ -151,3 +161,28 @@ function audio_views_handler_filter_tags
   }
   return $tags;
 }
+
+/**
+ * Field sort handler to convert numeric values in string fiels for sorting.
+ *
+ * For more info on how this works see: http://blog.feedmarker.com/2006/02/01/how-to-do-natural-alpha-numeric-sort-in-mysql/
+ */
+function audio_views_field_query_handler_numeric($fielddata, $fieldinfo, &$query) {
+  // This handler expects that the field will have 'notafield' => TRUE so that
+  // we can add in our field and not have worry about views overwriting it with
+  // the default.
+  $query->add_field($fielddata['field'] .' + 0', $fielddata['tablename'], $fielddata['queryname']);
+}
+
+/**
+ * Sort handler to convert numeric values in string fiels for sorting.
+ *
+ * For more info on how this works see: http://blog.feedmarker.com/2006/02/01/how-to-do-natural-alpha-numeric-sort-in-mysql/
+ */
+function audio_views_sort_handler_numeric_tag($op, &$query, $sortinfo, $sort) {
+  // We go to a bunch of trouble here to make sure we're adding the same field
+  // as audio_views_field_query_handler_numeric() would so that views doesn't
+  // duplicate it.
+  $query->add_orderby('', $sort['field'] .' + 0', $sort['sortorder'], $sortinfo['table'] .'_'. $sortinfo['field']);
+
+}

