Sort file names in lexicographical order of their extensions
Last Updated :
09 Mar, 2023
Given an array of strings Files[], representing name of some files, the task is to sort the array based on the lexicographical order of the extensions of the file names. If more than one files have the same extension, then sort them in lexicographically.
Examples:
Input: files[] = {“ajay.cpp”, “pchy.pdf”, “loki.docx”, “raju.zip” }
Output: ajay.cpp, loki.docx, pchy.pdf, raju.zip
Explanation:
Lexicographically sorted order of “cpp” < “docx” < “pdf” < “zip”
Input: files[] = {“abc.cpp”, “bcd.cpp”, “ab.cpp”, “efg.zip”}
Output: ab.cpp, abc.cpp, bcd.cpp, efg.zip
Explanation:
Since the file names { “abc.cpp”, “bcd.cpp”, “ab.cpp” } have same extension, they are sorted lexicographical order of their names.
Approach: Follow the steps below to solve the problem:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool custom(string s1, string s2)
{
size_t i = s1.find( '.' );
size_t j = s2.find( '.' );
string d = s1.substr(i + 1);
string e = s2.substr(j + 1);
if (d == e) {
return s1 < s2;
}
return d < e;
}
void sortfromextension(vector<string>& files)
{
sort(files.begin(), files.end(), custom);
for ( auto s : files) {
cout << s << ", " ;
}
}
int main()
{
vector<string> files
= { "ajay.cpp" , "pchy.pdf" ,
"loki.docx" , "raju.zip" };
sortfromextension(files);
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
class GFG
{
static void sortfromextension(String[] files)
{
Arrays.sort(files, new Comparator<String>(){
public int compare(String s1,String s2){
int i = s1.indexOf( '.' );
int j = s2.indexOf( '.' );
String d = s1.substring(i + 1 );
String e = s2.substring(j + 1 );
return (d.equals(e))?(s1.compareTo(s2)< 0 ?- 1 : 1 ):(d.compareTo(e)< 0 ?- 1 : 1 );
}
});
for ( int i = 0 ; i < files.length - 1 ; i++)
{
System.out.print(files[i] + ", " );
}
System.out.print(files[files.length - 1 ]);
}
public static void main (String[] args)
{
String[] files
= { "ajay.cpp" , "pchy.pdf" ,
"loki.docx" , "raju.zip" };
sortfromextension(files);
}
}
|
C#
using System;
using System.Linq;
class Program
{
static void SortFromExtension( string [] files)
{
Array.Sort(files, (s1, s2) =>
{
int i = s1.IndexOf( '.' );
int j = s2.IndexOf( '.' );
string d = s1.Substring(i + 1);
string e = s2.Substring(j + 1);
return (d == e) ? string .Compare(s1, s2, StringComparison.Ordinal) : string .Compare(d, e, StringComparison.Ordinal);
});
Console.WriteLine( string .Join( ", " , files));
}
static void Main( string [] args)
{
string [] files =
{
"ajay.cpp" , "pchy.pdf" ,
"loki.docx" , "raju.zip"
};
SortFromExtension(files);
}
}
|
Python3
import re
def SortFromExtension(files):
files.sort(key = lambda s: re.split( "\.([^.]+)" , s)[ 1 ])
print ( ", " .join(files))
if __name__ = = "__main__" :
files = [ "ajay.cpp" , "pchy.pdf" , "loki.docx" , "raju.zip" ]
SortFromExtension(files)
|
Javascript
function SortFromExtension(files) {
files.sort( function (a, b) {
let ext1 = a.split( '.' ).pop();
let ext2 = b.split( '.' ).pop();
if (ext1 < ext2) {
return -1;
} else if (ext1 > ext2) {
return 1;
} else {
return a.localeCompare(b);
}
});
console.log(files.join( ', ' ));
}
let files = [ "ajay.cpp" , "pchy.pdf" , "loki.docx" , "raju.zip" ];
SortFromExtension(files);
|
Output
ajay.cpp, loki.docx, pchy.pdf, raju.zip,
Time Complexity: O(N * log(N))
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...