Sorting a 2D array using the second column C++

I have to sort a 2D array of ranks and names (used as strings) based on the second column. I'm trying to sort it in alphabetical order but I can't seem to get it right. I'm still new to multi-dimensional arrays, so I'm still quite confused. I'm using Bubble Sort, and it is sorting the names, but not in the right order.

this is the array I'm trying to sort:

const int ROWS=6; const int COLS=2; string names [ROWS][COLS] = { "1", "Jacob", "2", "Michael", "3", "Joshua", "4", "Matthew", "5", "Ethan", "6", "Andrew"};

this is what I get:

2 Michael 4 Matthew 3 Joshua 1 Jacob 6 Andrew 5 Ethan

and this is the sort function I'm using:

void sort (string names [][COLS], int size) { int i,j; // i = rows string hold1, hold2; for (i=0; i < size-1; i++) { for (j=0; j < size; j++) { if (names [i][1] > names [j][1]) //names = col 1 , rank = col 0 { hold1 = names [i][1]; hold2 = names [i][0]; names [i][1] = names [j][1]; names [i][0] = names [j][0]; names [j][1] = hold1; names [j][0] = hold2; } } } }

Thanks in advance.


You're actually swapping the elements back and forth. You need to make sure that when you're Bubble Sorting you're only comparing an element with a later element. That is:

for (i=0; i < size-1; i++) { for (j=i+1; j < size; j++) ^^^^ { // same as before } }

Note that we can take advantage of standard utilities to make this code a lot easier to understand. The line I wrote as //same as before... what you're doing there is just swapping names[i] and names[j], which we can spell:

std::swap(names[i], names[j]);

That's just easier to understand and less error-prone.


You got the bubble sort wrong. try this :

for (i=0; i<size; i++) { for (j=0; j < size-1; j++) { if (names [j][1] > names [j+1][1]) { hold1 = names [j][1]; hold2 = names [j][0]; names [j][1] = names [j+1][1]; names [j][0] = names [j+1][0]; names [j+1][1] = hold1; names [j+1][0] = hold2; } } }


  • When to use Abstract Classes and Interfaces? [closed]
  • How to convert a Java web application to Grails?
  • RestKit - Post object and update its attributes
  • Unable to extract date of birth from a given format
  • Reorder columns in a matrix
  • Formatting dates using XPath
  • Stuck on parsing a JSON response to a Java Object in Android
  • Join an array of strings with a different final delimiter
  • Difference between Debug and Release folders
  • Capybara fill_in only works with field id, why?
  • Data read separately fscanf
  • SSRS mdx report: use dimension on columns
  • how to set hue value of some pixel with opencv
  • sort array value in alphabetical order [duplicate]
  • Binary to Decimal Conversion using Divide-and-conquer
  • R Data table setkey - error some columns are not in the data.table
  • counting islands in R csv
  • Oracle insert into returning
  • Using gpu::GpuMat in OpenCV C++
  • table with scrollable body and colgroup does not work
  • Change array dimensions at runtime
  • Sort Columns For Dynamic Pivot
  • How to count spring coil turns?
  • Unable to populate DataGrid
  • Referring to individual variables in … with dplyr quos
  • Connect to OLEDB data source from Java?
  • Refresh other frame, from another frame. Jquery
  • Retrieving value from sql ExecuteScalar()
  • vba code to select only visible cells in specific column except heading
  • How to set my toolbar fixed while scrolling android
  • Do I've to free mysql result after storing it?
  • Rearranging Cells in UITableView Bug & Saving Changes
  • AT Commands to Send SMS not working in Windows 8.1
  • Transpose CSV data with awk (pivot transformation)
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • apache spark aggregate function using min value
  • Sorting a 2D array using the second column C++
  • java string with new operator and a literal