diff --git a/applets/notification_area/na-tray-child.c b/applets/notification_area/na-tray-child.c
index 30f3d58..d7a1dfd 100644
--- a/applets/notification_area/na-tray-child.c
+++ b/applets/notification_area/na-tray-child.c
@@ -335,6 +335,86 @@ na_tray_child_get_title (NaTrayChild *child)
   return retval;
 }
 
+/* from libwnck xutils.c */
+static char *
+latin1_to_utf8 (const char *latin1)
+{
+  GString *str;
+  const char *p;
+
+  str = g_string_new (NULL);
+
+  p = latin1;
+  while (*p)
+    {
+      g_string_append_unichar (str, (gunichar) *p);
+      ++p;
+    }
+
+  return g_string_free (str, FALSE);
+}
+
+/* derived from libwnck xutils.c */
+static void
+_get_wmclass (Display *xdisplay,
+              Window   xwindow,
+              char   **res_class,
+              char   **res_name)
+{
+  XClassHint ch;
+  char *retval;
+
+  gdk_error_trap_push ();
+
+  ch.res_name = NULL;
+  ch.res_class = NULL;
+
+  XGetClassHint (xdisplay, xwindow, &ch);
+
+  gdk_error_trap_pop ();
+
+  retval = NULL;
+
+  if (res_class)
+    *res_class = NULL;
+
+  if (res_name)
+    *res_name = NULL;
+
+  if (ch.res_name)
+    {
+      if (res_name)
+        *res_name = latin1_to_utf8 (ch.res_name);
+
+      XFree (ch.res_name);
+    }
+
+  if (ch.res_class)
+    {
+      if (res_class)
+        *res_class = latin1_to_utf8 (ch.res_class);
+
+      XFree (ch.res_class);
+    }
+}
+
+void
+na_tray_child_get_wm_class (NaTrayChild *child,
+                            char **res_name,
+                            char **res_class)
+{
+  GdkDisplay *display;
+
+  g_return_if_fail (NA_IS_TRAY_CHILD (child));
+
+  display = gtk_widget_get_display (GTK_WIDGET (child));
+
+  _get_wmclass (GDK_DISPLAY_XDISPLAY (display),
+                child->icon_window,
+                res_class,
+                res_name);
+}
+
 gboolean
 na_tray_child_is_composited (NaTrayChild *child)
 {
diff --git a/applets/notification_area/na-tray-child.h b/applets/notification_area/na-tray-child.h
index f9ed5bd..8bae952 100644
--- a/applets/notification_area/na-tray-child.h
+++ b/applets/notification_area/na-tray-child.h
@@ -59,6 +59,9 @@ GtkWidget      *na_tray_child_new           (GdkScreen   *screen,
 char           *na_tray_child_get_title     (NaTrayChild *child);
 gboolean        na_tray_child_is_composited (NaTrayChild *child);
 void            na_tray_child_force_redraw  (NaTrayChild *child);
+void            na_tray_child_get_wm_class  (NaTrayChild *child,
+					     char       **res_name,
+					     char       **res_class);
 
 G_END_DECLS
 
diff --git a/applets/notification_area/na-tray.c b/applets/notification_area/na-tray.c
index b2259d5..a138f27 100644
--- a/applets/notification_area/na-tray.c
+++ b/applets/notification_area/na-tray.c
@@ -124,6 +124,9 @@ tray_added (NaTrayManager *manager,
 {
   NaTray *tray;
   NaTrayPrivate *priv;
+  GList *l;
+  int position;
+  char *class_a;
 
   tray = get_tray (trays_screen);
   if (tray == NULL)
@@ -135,7 +138,31 @@ tray_added (NaTrayManager *manager,
   
   g_hash_table_insert (trays_screen->icon_table, icon, tray);
 
+  class_a = NULL;
+  na_tray_child_get_wm_class (NA_TRAY_CHILD (icon), NULL, &class_a);
+
+  l = gtk_container_get_children (GTK_CONTAINER (priv->box));
+  position = 0;
+  while (l != NULL)
+    {
+      char *class_b;
+
+      class_b = NULL;
+      na_tray_child_get_wm_class (NA_TRAY_CHILD (l->data), NULL, &class_b);
+
+      if (g_strcmp0 (class_a, class_b) >= 0)
+        {
+          g_free (class_b);
+          break;
+        }
+
+      g_free (class_b);
+      l = l->next;
+      position++;
+    }
+  g_free (class_a);
   gtk_box_pack_end (GTK_BOX (priv->box), icon, FALSE, FALSE, 0);
+  gtk_box_reorder_child (GTK_BOX (priv->box), icon, position);
 
   gtk_widget_show (icon);
 }
