title: Hashtable sorting using bubble sort c++
body: <p>I have a problem with sorting my Hashtable. I have methods to compare numbers, letters and bubble sort algorithm. The sorting output is not my desired one because it prints the hashtable by insert-order (first inserted - first printed) and what i want is sorting by the key in ascending order or by the value is ascending as well. Keys are Integers and values are Strings.
  Here's the code:</p>
  
  <pre><code>#include&lt;iostream&gt;
  #include&lt;string&gt;
  
  using namespace std;
  const int size = 100;
  
  class Binding
  {
      public:
          int key;
          string value;
          Binding *next;
  
          Binding(int key, string value)
          {
              this-&gt;key = key;
              this-&gt;value = value;
              this-&gt;next = NULL;
          }
  };
  
  class HashTable
  {
      private:
          Binding** tarray;
  
      public:
          HashTable()
          {
              array = new Binding*[size];
              for(int i = 0; i &lt; size; i++)
              {                
                  array[i] = NULL;
              }
          }
  
          int insert(int key, string value)           
          {
              int hash = (key % size);
              Binding *record = array[hash];
              Binding *previous = NULL;
  
              if(record != NULL)
              {
                  previous = record;
                  record = record-&gt;next;
              }
              else if(record == NULL)
              {
                  record = new Binding(key, value);
  
                  if (previous == NULL)
                  {
                      array[hash] = record;
                  }
                  else
                  {
                      previous-&gt;next = record;
                  }
              }
              else
              {
                  record-&gt;value = value;
              }
          }
  
          int compareLetters(const void *a, const void *b)
          {
               Binding *A = (Binding*)a;
               Binding* B = (Binding*)b;
               int compare = strcmp(A-&gt;letter, B-&gt;letter);
  
               if(compare == 0)
                    return 0;
               else if(compare &gt; 0)
                   return 1;
               else if(compare &lt; 0)
                   return -1;
          }
  
          int compareNumbers(const void *a, const void *b)
          {
              Binding *A = (Binding*)a;
              Binding *B = (Binding*)b;
              if(A-&gt;key &gt; B-&gt;key)
                  return 1;
              else if(A-&gt;key &lt; B-&gt;key)
                  return -1;
              else 
                  return 0;
          }
  
          void bubble_sort()
          {
              Binding *temp;
              for(int i=1; i&lt;size; i++)
              {
                  for(int j=0; j&lt;size - i; j++)
                  {
                      if(compareNumbers(&amp;array[j], &amp;array[j+1]) == 1)
                      {
                          temp = array[j];
                          array[j] = array[j+1];
                          array[j+1] = temp;
                      }
                  }   
              }
          } 
  </code></pre>