Hướng dẫn alphanumeric sorting in python

Alpha Numeric Sorting

Nội dung chính

  • Alpha Numeric Sorting
  • How do you sort alphanumeric values in Python?
  • What is alphanumeric sorting?
  • How do you sort alphanumeric characters?
  • What is alphanumeric order example?

LEVEL:Beginner

Description

Given text comprising of words and numbers, sort them both in ascending order and print them in a manner that a word is followed by a number. Words can be in upper or lower case. You have to convert them into lowercase and then sort and print them.

Input Format

First line contains total number of test cases, denoted by N
Next N lines, each contains a text in which words are at odd position and numbers are at even position and are delimited by space
Constraints:
1. Text starts with a word
2. Count of words and numbers are the same.
3. Duplicate elements are not allowed
4. Words should be printed in lower case.
5. No special characters allowed in text.

Output Format

Words and numbers sorted in ascending order and printed in a manner that a word is followed by a number.

Example 1:

Input

2
Sagar 35 sanjay 12 ganesH 53 ramesh 19
Ganesh 59 suresh 19 rakesh 26 laliT 96 

Output

ganesh 12 ramesh 19 sagar 35 sanjay 53 
ganesh 19 lalit 26 rakesh 59 suresh 96 

Example 2:

Input

3
SteveJobs 40 TimBerners-Lee 70 BillGates 45 
JamesGosling 60  LinusTorvalds 35 RichardStallman 54
SatyaNadellea 30 SundarPichai 28 MarkZuckerberg 35 AzimPremji 30

Output

billgates 40 stevejobs 45 timberners-lee 70 
jamesgosling 35 linustorvalds 54 richardstallman 60 
azimpremji 28 markzuckerberg 30 satyanadellea 30 sundarpichai 35

Example 3:

Input

2
Microsoft 78 Google 28 Facebook 34
Oracle 36 Cognizant 73 IBM 67 TCS 63

Output

facebook 28 google 34 microsoft 78 
cognizant 36 ibm 63 oracle 67 tcs 73

Hướng dẫn alphanumeric sorting in python


Approach

Seggregate the even indices and odd indices to 2 different arrays.
Convert the words into lowecase words.
Sort words and numbers differenlty.
Iterate a loop and print the values alternately.

Note :

Let us know if you can come up with a better approach, mail us at Your approach will be reviewed and posted with credits to you.

Problem : Alpha Numeric Sorting

Given text comprising of words and numbers, sort them both in ascending order and print them in a manner that a word is followed by a number. Words can be in upper or lower case. You have to convert them into lowercase and then sort and print them.

Input Format:

First line contains total number of test cases, denoted by N
Next N lines, each contains a text in which words are at odd position and numbers are at even position and are delimited by space

Output Format:

Words and numbers sorted in ascending order and printed in a manner that a word is followed by a number.

Constraints:

1. Text starts with a word

2. Count of words and numbers are the same.

3. Duplicate elements are not allowed 

4. Words should be printed in lower case. 

5. No special characters allowed in text.

Sample Input and Output

SNo.InputOutput
1
2
Sagar 35 sanjay 12 ganesH 53 ramesh 19
Ganesh 59 suresh 19 rakesh 26 laliT 96

ganesh 12 ramesh 19 sagar 35 sanjay 53
ganesh 19 lalit 26 rakesh 59 suresh 96

SOLUTION:-

#include

#include

#include

int main()

{

char s[100][100],t[100],e[100][100],o[100][100];

int i=0,j,k=0,swap,p,n,c,d,m=0,q,w,a[1000];

do

{

scanf("%s %d",&s[i],&a[i]);

i++;

}

while(getchar() != '\n');

for(w=0;w

{

q=0;

while(s[w][q] != '\0')

{

s[w][q] = tolower(s[w][q]);

q++;

}

}

for(w=1;w

{

for(j=1;j

    {

    if(strcmp(s[j-1],s[j])>0)

    {

    strcpy(t,s[j-1]);

    strcpy(s[j-1],s[j]);

    strcpy(s[j],t);

    }

    }

}

for (c = 0 ; c < i-1; c++)

{

    for (d = 0 ; d < i - c - 1 ; d++)

    {

      if (a[d] > a[d+1]) 

      {

        swap=a[d];

        a[d]=a[d+1];

        a[d+1]=swap;

      }

    }

}

for(w=0;w

{

printf("%s %d ",s[w],a[w]);

}

return 0;

}

Happy Coding :) :')

How do you sort alphanumeric values in Python?

Now there are again two ways of using sorted()..

Method #1 : Using key function. # Python3 program to Sort list. # containing alpha and numeric values. def sort(lst): return sorted (lst, key = str ) ... .

Method #2 : lambda. # Python3 program to Sort list. # containing alpha and numeric values. def sort(lst):.

What is alphanumeric sorting?

SORT-TYPES is an alphanumeric property that can be repeated for each column contained in the grid. The value determines the sorting property of the column as shown in the table. Many of these values are similar to those for the DATA-TYPES property.

How do you sort alphanumeric characters?

You can't use the default String compareTo() instead need compare the Strings following the below algorithm..

Loop through the first and second String character by character and get a chunk of all strings or numbers..

Check if the chunks are numbers or strings..

If numbers sort numerically else use String compareTo().

What is alphanumeric order example?

Order them by the first digit. For example, 11 would come before 2. The number 22 would come before 3. The number 33 would come before 4.