Sort vector of Numeric Strings in ascending order
Last Updated :
02 Jun, 2022
Given a vector of numeric strings arr[], the task is to sort the given vector of numeric strings in ascending order.
Examples:
Input: arr[] = {“120000”, “2”, “33”}
Output: {“2”, “33”, “120000”}
Input: arr[] = {“120”, “2”, “3”}
Output: {“2”, “3”, “120”}
Approach: The sort() function in C++ STL is able to sort vector of strings if and only if it contains single numeric character for example, { ‘1’, ‘ ‘} but to sort numeric vector of string with multiple character for example, {’12’, ’56’, ’14’ } one should write own comparator inside sort() function. The comparator function to sort in ascending order logic goes as:
Let’s build a comparator function considering the two cases given below:
- Case 1: If the length of the string is not the same then place a smaller length string in first place for example, {’12’, ‘2’} place ‘2’ before ’12’ as ‘2’ is smaller than ’12’.
- Case 2: If length is the same then place the string which is numerically smaller for example, ‘1’, ‘2’ place ‘1’ before ‘2’.
Below is the C++ program to implement the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool myCmp(string s1, string s2)
{
if (s1.size() == s2.size()) {
return s1 < s2;
}
else {
return s1.size() < s2.size();
}
}
int main()
{
vector<string> v
= { "12" , "2" , "10" , "6" , "4" , "99" , "12" };
sort(v.begin(), v.end(), myCmp);
for ( auto it : v) {
cout << it << " " ;
}
cout << "\n" ;
}
|
Java
import java.util.*;
class GFG{
static List<String> sort(List<String> list)
{
Comparator<String> cmp = (o1, o2) -> {
if (o1.length() == o2.length()) {
return Integer.valueOf(o1)-Integer.valueOf(o2);
}
else {
return o1.length()-o2.length();
}
};
Collections.sort(list, cmp);
return list;
}
public static void main(String[] args)
{
List<String> v
= Arrays.asList( "12" , "2" , "10" , "6" , "4" , "99" , "12" );
v = sort(v);
for (String it : v) {
System.out.print(it+ " " );
}
}
}
|
Python3
from functools import cmp_to_key
def myCmp(s1, s2):
if ( len (s1) = = len (s2)):
return int (s1) - int (s2)
else :
return len (s1) - len (s2)
v = [ "12" , "2" , "10" , "6" , "4" , "99" , "12" ]
v.sort(key = cmp_to_key(myCmp))
for i in range ( len (v)) :
print (v[i],end = " " )
|
C#
using System;
class GFG
{
public static int myCmp( string s1, string s2)
{
if (s1.Length == s2.Length) {
return Convert.ToInt32(s1) - Convert.ToInt32(s2);
}
else {
return s1.Length-s2.Length;
}
}
public static void Main()
{
string [] v = { "12" , "2" , "10" , "6" , "4" , "99" , "12" };
Array.Sort< string >(
v, delegate ( string m, string n) { return myCmp(m,n); });
for ( int i = 0; i < v.Length; i++)
{
Console.Write(v[i]+ " " );
}
}
}
|
Javascript
<script>
function myCmp(s1, s2) {
if (s1.length == s2.length) {
return parseInt(s1) - parseInt(s2);
}
else {
return s1.length - s2.length;
}
}
let v
= [ "12" , "2" , "10" , "6" , "4" , "99" , "12" ];
v.sort(myCmp)
for (let i = 0; i < v.length; i++) {
document.write(v[i] + " " )
}
</script>
|
Output:
2 4 6 10 12 12 99
Time Complexity: O(N*log N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...