1) Write a function, input as an array of values and output should be sorted by integer part
of the string.
Input array: ['ab-1', 'ab-021', 'ab-201', 'ab-011', 'ab-12','ab-22')
Sort values in ascending order based on integer part after the hyphen character (-).
Expected output should be as, ['ab-1', 'ab-011', 'ab-12', 'ab-021', 'ab-22', 'ab-201']
Answers
Answered by
0
Answer:
array=['ab-1',' ab-021','ab-201', 'ab-011', 'ab-12','ab-22']
b=[]
for i in array:
pos=i.find('-')
pos+=1
b.append(i[pos:])
c=[]
for i in b:
if(i[0]=='0'):
c.append(int(i[1:]))
else:
c.append(int(i))
c.sort()
p=[]
for i in c:
for j in range(len(b)):
if(b[j].startswith('0') and str(i)==b[j][1:]):
p.append(b[j])
elif(str(i)==b[j]):
p.append(b[j])
f=[]
for j in p:
for i in range(len(array)):
pos=array[i].find('-')
pos+=1
if(str(j)==array[i][pos:]):
f.append(array[i])
print(f)
Similar questions