0

This is a interview question . Let's say I have list of international phone numbers like below and I want to sort them in ascending or descending.

+91-9618229418
+1(608) 666-555
+1(408) 666-555
(308) 666-555

So excepted result is

(308) 666-555
+1(408) 666-555
+1(608) 666-555
+91-9618229418

Can someone help me logic

D7thename
  • 21
  • 1
  • 7
  • Can they be stripped down to purely numbers? – Matthew Gaiser Sep 10 '19 at 16:50
  • No, final result should preserve all symbols and spaces – D7thename Sep 10 '19 at 16:51
  • You have to write your own comparison function that will convert the phone number strings into a format that you can directly compare. That means stripping out the "+xx" values, removing parentheses and other non-numeric characters. You keep the original strings, but compare the stripped-down versions. – Jim Mischel Sep 10 '19 at 20:16

2 Answers2

2

You could use a TreeMap.

 public static void main(String args[])
    {
        TreeMap<Long, String> sorted = new TreeMap<>();
        String[] numbers = {"+91-9618229418",
                "+1(608) 666-555",
                "+1(408) 666-555",
                "(308) 666-555"};

        for (int i = 0; i < numbers.length; i++){
            long number = Long.parseLong(numbers[i].replaceAll("[^0-9]", ""));
            sorted.put(number, numbers[i]);
        }
        for(Map.Entry<Long,String> entry : sorted.entrySet()) {
            System.out.println(entry.getValue());
        }
    }

The end result is

(308) 666-555
+1(408) 666-555
+1(608) 666-555
+91-9618229418

The why: A TreeMap is a sorted mapping which sorts either based on natural ordering (which is what is used here as we are just sorting numbers by value) or using a provided Comparator.

I chose a TreeMap because to sort numbers, all the non-number characters must be removed. However, as the requirement is for keeping the formatting the same, the solution is to strip out the non-numerical characters, sort those, and then use them to point back to the original strings of numbers.

Matthew Gaiser
  • 4,558
  • 1
  • 18
  • 35
0

If not all phone numbers are 10 digits (not including country code). Let me know if this helps.

I used this link to sort.


listOfNums = ['+91-9618229418', '+1(608) 666-555', '+1(408) 666-555', '(308) 666-555', '+1(608) 636-555']
phList = []
for n in listOfNums:
    if '+' in n:
        specialChar = None
        for i in range(1,len(n)): #All character other than '+'
            #Below if statement will find the first non-int index.
            #i.e., the index of the character that spereates international code
            #from the actual phone number
            if n[i] not in '0123456789':
                specialChar = i
                i = len(n)+1
        tempStr = n[-(len(n)-specialChar):] #Get the subtring without international code

        ph = "".join(filter( lambda x: x in '0123456789', str(tempStr) ))
        #Strip it so that you only have the ints (for sorting)
        phList.append(ph)

    else:#if there is not international code
        ph = ph = "".join(filter( lambda x: x in '0123456789', str(n) ))
        phList.append(ph)


phList, listOfNums = zip(*sorted(zip(phList, listOfNums)))
print (listOfNums)

Output:
('(308) 666-555', '+1(408) 666-555', '+1(608) 636-555', '+1(608) 666-555', '+91-9618229418')

If All Phone Numbers are 10 digits (not including country code). I have edited your example so that they match this criteria.

listOfNums = ['+91-9618229418','+1(608) 666-5555','+1(408) 666-5555','(308) 666-5555']
phList = []
for n in listOfNums:

    ph =  "".join(filter( lambda x: x in '0123456789', str(n) ))[-10:]
    #Creates ph with only characters between 0-9 and gets the last 10 characters    
    phList.append(ph)

phList, listOfNums = zip(*sorted(zip(phList, listOfNums)))
print(listOfNums)

Output:
('(308) 666-5555', '+1(408) 666-5555', '+1(608) 666-5555', '+91-9618229418')
Harsha
  • 353
  • 1
  • 15