x-ice
Oct 27th, 2005, 04:27 PM
I have a string that i want to insert into an unsorted list in the correct place alphabetically. Here is what i mean... The list:
Peter (1)
George (2)
Mike (3)
Bill (4)
Jason (5)
Carl (6)
Alex (7)
If i wanted to insert the string 'Dan' (String X) into this list the logical answer would be to compare the strings in the list. This is how i would do it.
Compare string 1 and 2.
String 1 is more than (alphabetically) string 2, so continue
Compare string 2 and 3
String 2 is less than (alphabetically) string 3, so
Compare string 2 and string X
String X is less than (alphabetically) string 2, so continue
Compare string 3 and 4
String 3 is more than (alphabetically) string 4, so continue
Compare string 4 and 5
String 4 is less than (alphabetically) string 5, so
Compare string 4 and string X
String X is more than (alphabetically) string 4, so
Move string 5,6,7 to element 6,7,8 in list, leaving element 5 blank
Insert string X into element 5 (blank space)
My question is how would i compare the strings?
Peter (1)
George (2)
Mike (3)
Bill (4)
Jason (5)
Carl (6)
Alex (7)
If i wanted to insert the string 'Dan' (String X) into this list the logical answer would be to compare the strings in the list. This is how i would do it.
Compare string 1 and 2.
String 1 is more than (alphabetically) string 2, so continue
Compare string 2 and 3
String 2 is less than (alphabetically) string 3, so
Compare string 2 and string X
String X is less than (alphabetically) string 2, so continue
Compare string 3 and 4
String 3 is more than (alphabetically) string 4, so continue
Compare string 4 and 5
String 4 is less than (alphabetically) string 5, so
Compare string 4 and string X
String X is more than (alphabetically) string 4, so
Move string 5,6,7 to element 6,7,8 in list, leaving element 5 blank
Insert string X into element 5 (blank space)
My question is how would i compare the strings?