In case of ASCII representation of characters, in which order will a computer sort the strings 23, A1, 1A, a2, 2a, aA and Aa ?
[amp_mcq option1=”1A<23<2aThis question was previously asked in
UPSC CISF-AC-EXE – 2020
Computers typically sort strings based on the numerical values assigned to characters in a character encoding standard like ASCII. In ASCII:
– Digits (‘0’-‘9’) have values 48-57.
– Uppercase letters (‘A’-‘Z’) have values 65-90.
– Lowercase letters (‘a’-‘z’) have values 97-122.
Thus, digits come before uppercase letters, which come before lowercase letters. Sorting is done character by character from left to right.
Let’s list the strings and their first characters’ ASCII values:
– 23: ‘2’ (50)
– A1: ‘A’ (65)
– 1A: ‘1’ (49)
– a2: ‘a’ (97)
– 2a: ‘2’ (50)
– aA: ‘a’ (97)
– Aa: ‘A’ (65)
Based on the first character, the order is 1A (49), then 23 and 2a (both 50), then A1 and Aa (both 65), then a2 and aA (both 97).
Now compare within groups with the same first character:
– 23 vs 2a: ‘3’ (51) vs ‘a’ (97). ‘3’ < 'a', so 23 comes before 2a. Order: 23, 2a. - A1 vs Aa: '1' (49) vs 'a' (97). '1' < 'a', so A1 comes before Aa. Order: A1, Aa. - a2 vs aA: '2' (50) vs 'A' (65). '2' < 'A', so a2 comes before aA. Order: a2, aA. Combining the ordered groups based on the first character's order: 1A (starts with '1') 23, 2a (start with '2') A1, Aa (start with 'A') a2, aA (start with 'a') The final sorted order is 1A, 23, 2a, A1, Aa, a2, aA. This matches option A. – ASCII sorting is based on the numerical value of each character.
– Sorting is lexicographical, comparing characters from left to right.
– Digit characters have lower ASCII values than uppercase letters, which have lower values than lowercase letters.