Sort vector of Numeric Strings in ascending order Last Updated : 02 Jun, 2022 Comments Improve Suggest changes Like Article Like Report 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++ // C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Comparator Function bool myCmp(string s1, string s2) { // If size of numeric strings // are same the put lowest value // first if (s1.size() == s2.size()) { return s1 < s2; } // If size is not same put the // numeric string with less // number of digits first else { return s1.size() < s2.size(); } } // Driver Code int main() { vector<string> v = { "12", "2", "10", "6", "4", "99", "12" }; // Calling sort function with // custom comparator sort(v.begin(), v.end(), myCmp); // Print the vector values after // sorting for (auto it : v) { cout << it << " "; } cout << "\n"; } Java // Java program for the above approach import java.util.*; class GFG{ // Comparator Function static List<String> sort(List<String> list) { Comparator<String> cmp = (o1, o2) -> { // If size of numeric Strings // are same the put lowest value // first if (o1.length() == o2.length()) { return Integer.valueOf(o1)-Integer.valueOf(o2); } // If size is not same put the // numeric String with less // number of digits first else { return o1.length()-o2.length(); } }; Collections.sort(list, cmp); return list; } // Driver Code public static void main(String[] args) { List<String> v = Arrays.asList( "12", "2", "10", "6", "4", "99", "12" ); // Calling sort function with // custom comparator v = sort(v); // Print the vector values after // sorting for (String it : v) { System.out.print(it+ " "); } } } // This code is contributed by 29AjayKumar Python3 # Python3 Program to implement # the above approach # Comparator Function from functools import cmp_to_key def myCmp(s1, s2): # If size of numeric strings # are same the put lowest value # first if (len(s1) == len(s2)): return int(s1) - int(s2) # If size is not same put the # numeric string with less # number of digits first else: return len(s1) - len(s2) # Driver Code v = ["12", "2", "10", "6", "4", "99", "12"] # Calling sort function with # custom comparator v.sort(key = cmp_to_key(myCmp)) # Print the vector values after # sorting for i in range(len(v)) : print(v[i],end=" ") # This code is contributed by shinjanpatra C# // C# program for the above approach using System; class GFG { // Comparator Function public static int myCmp(string s1, string s2) { // If size of numeric strings // are same the put lowest value // first if (s1.Length == s2.Length) { return Convert.ToInt32(s1) - Convert.ToInt32(s2); } // If size is not same put the // numeric string with less // number of digits first else { return s1.Length-s2.Length; } } // Driver code public static void Main() { string[] v = { "12", "2", "10", "6", "4", "99", "12" }; // Calling sort function with // custom comparator Array.Sort<string>( v, delegate(string m, string n) { return myCmp(m,n); }); // Print the vector values after // sorting for (int i = 0; i < v.Length; i++) { Console.Write(v[i]+" "); } } } // This code is contributed by Pushpesh Raj. JavaScript <script> // JavaScript Program to implement // the above approach // Comparator Function function myCmp(s1, s2) { // If size of numeric strings // are same the put lowest value // first if (s1.length == s2.length) { return parseInt(s1) - parseInt(s2); } // If size is not same put the // numeric string with less // number of digits first else { return s1.length - s2.length; } } // Driver Code let v = ["12", "2", "10", "6", "4", "99", "12"]; // Calling sort function with // custom comparator v.sort(myCmp) // Print the vector values after // sorting for (let i = 0; i < v.length; i++) { document.write(v[i] + " ") } // This code is contributed by Potta Lokesh </script> Output: 2 4 6 10 12 12 99 Time Complexity: O(N*log N)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Sort vector of Numeric Strings in ascending order bhat96 Follow Improve Article Tags : Strings Sorting DSA Arrays Blogathon-2021 +1 More Practice Tags : ArraysSortingStrings Similar Reads Sort strings on the basis of their numeric part Given a sentence S of size N where each word is a concatenation of a numeric part followed by a bunch of characters. The task is to arrange the words in increasing order of their numeric part. Examples: Input: S = "24-amazing 7-coders 11-are"Output: coders are amazingExplanation: order of numeric va 8 min read Program to sort string in descending order Given a string, sort it in descending order. Examples: Input : alkasingh Output : snlkihgaa Input : nupursingh Output : uusrpnnihg Input : geeksforgeeks Output : ssrokkggfeeee Recommended PracticeSort the string in descending orderTry It! A simple solution is to use library sort function std::sort() 7 min read Sort an array of Roman Numerals in ascending order Given an array arr[] of N Roman Numerals, the task is to sort these Roman Numerals in ascending order.Examples: Input: arr[] = { "MCMIV", "MIV", "MCM", "MMIV" } Output: MIV MCM MCMIV MMIV Explanation: The roman numerals in their corresponding decimal system are: MIV: 1004 MCM: 1900 MCMIV: 1904 MMIV: 12 min read Sort an Array of Strings in Lexicographical order Given an array of strings arr[] of size n, the task is to sort all the strings in lexicographical order. Examples:Input: arr[] = ["banana", "apple", "cherry"]Output: ["apple", "banana", "cherry"]Explanation: All strings are sorted alphabetically. "apple" comes before "banana", and "banana" before "c 11 min read Python - Sort words of sentence in ascending order Sorting words in a sentence in ascending order can be useful for tasks like text analysis, data preprocessing, or even fun applications like creating word puzzles. Itâs simple to achieve this using Python. In this article, we will explore different methods to do this.Using sorted() with SplitThe mos 3 min read C Program to Sort an array of names or strings Given an array of strings in which all characters are of the same case, write a C function to sort them alphabetically. The idea is to use qsort() in C and write a comparison function that uses strcmp() to compare two strings. C #include <stdio.h> #include <stdlib.h> #include <string. 2 min read Sort a list of strings in lexicographical order in Python We are given a list of strings and our task is to sort them in lexicographical order, which means alphabetical order either from A to Z (ascending) or Z to A (descending). This is a common operation when organizing or comparing strings in Python. For example:Input: ["banana", "apple", "cherry", "dat 2 min read Sort a string in increasing order of given priorities Given an alphanumeric string S of length N, the task is to sort the string in increasing order of their priority based on the following conditions: Characters with even ASCII values have higher priority than characters with odd ASCII values.Even digits have higher priority than odd digits.Digits hav 8 min read PHP Sort array of strings in natural and standard orders You are given an array of strings. You have to sort the given array in standard way (case of alphabets matters) as well as natural way (alphabet case does not matter).Input : arr[] = {"Geeks", "for", "geeks"}Output : Standard sorting: Geeks for geeks Natural sorting: for Geeks geeks Input : arr[] = 2 min read Sort an array of strings according to string lengths We are given an array of strings, we need to sort the array in increasing order of string lengths.Examples: Input : {"GeeksforGeeeks", "I", "from", "am"}Output : I am from GeeksforGeeks Input : {"You", "are", "beautiful", "looking"}Output : You are looking beautiful A simple solution is to write our 10 min read Like