Added Numeric String Sort
Sorts an array of Numeric strings.
An array of strings like 1,2,3,10,20,30,100,200,300 are sorted as 1,10,100,2,20,200,3,30,300 Conventionally.
This programme sorts them in the correct numeric order i.e 1,2,3,10,20,30,100,200,300
2017-12-30 04:32:44 +08:00
|
|
|
//Using general algorithms to sort a collection of strings results in alphanumeric sort.
|
|
|
|
//If it is a numeric string, it leads to unnatural sorting
|
|
|
|
|
|
|
|
//eg, an array of strings 1,10,100,2,20,200,3,30,300
|
|
|
|
//would be sorted in that same order by using conventional sorting,
|
|
|
|
//even though we know the correct sorting order is 1,2,3,10,20,30,100,200,300
|
|
|
|
|
|
|
|
//This Programme uses a comparator to sort the array in Numerical order instead of Alphanumeric order
|
|
|
|
|
2019-08-21 10:10:08 +08:00
|
|
|
#include <iostream>
|
|
|
|
#include <string>
|
|
|
|
#include <algorithm>
|
Added Numeric String Sort
Sorts an array of Numeric strings.
An array of strings like 1,2,3,10,20,30,100,200,300 are sorted as 1,10,100,2,20,200,3,30,300 Conventionally.
This programme sorts them in the correct numeric order i.e 1,2,3,10,20,30,100,200,300
2017-12-30 04:32:44 +08:00
|
|
|
using namespace std;
|
|
|
|
|
2019-08-21 10:10:08 +08:00
|
|
|
bool NumericSort(string a, string b)
|
Added Numeric String Sort
Sorts an array of Numeric strings.
An array of strings like 1,2,3,10,20,30,100,200,300 are sorted as 1,10,100,2,20,200,3,30,300 Conventionally.
This programme sorts them in the correct numeric order i.e 1,2,3,10,20,30,100,200,300
2017-12-30 04:32:44 +08:00
|
|
|
{
|
2019-08-21 10:10:08 +08:00
|
|
|
while (a[0] == '0')
|
Added Numeric String Sort
Sorts an array of Numeric strings.
An array of strings like 1,2,3,10,20,30,100,200,300 are sorted as 1,10,100,2,20,200,3,30,300 Conventionally.
This programme sorts them in the correct numeric order i.e 1,2,3,10,20,30,100,200,300
2017-12-30 04:32:44 +08:00
|
|
|
{
|
|
|
|
a.erase(a.begin());
|
|
|
|
}
|
2019-08-21 10:10:08 +08:00
|
|
|
while (b[0] == '0')
|
Added Numeric String Sort
Sorts an array of Numeric strings.
An array of strings like 1,2,3,10,20,30,100,200,300 are sorted as 1,10,100,2,20,200,3,30,300 Conventionally.
This programme sorts them in the correct numeric order i.e 1,2,3,10,20,30,100,200,300
2017-12-30 04:32:44 +08:00
|
|
|
{
|
|
|
|
b.erase(b.begin());
|
2019-08-21 10:10:08 +08:00
|
|
|
}
|
|
|
|
int n = a.length();
|
|
|
|
int m = b.length();
|
|
|
|
if (n == m)
|
|
|
|
return a < b;
|
|
|
|
return n < m;
|
Added Numeric String Sort
Sorts an array of Numeric strings.
An array of strings like 1,2,3,10,20,30,100,200,300 are sorted as 1,10,100,2,20,200,3,30,300 Conventionally.
This programme sorts them in the correct numeric order i.e 1,2,3,10,20,30,100,200,300
2017-12-30 04:32:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
|
2019-08-21 10:10:08 +08:00
|
|
|
int n;
|
|
|
|
cout << "Enter number of elements to be sorted Numerically\n";
|
|
|
|
cin >> n;
|
Added Numeric String Sort
Sorts an array of Numeric strings.
An array of strings like 1,2,3,10,20,30,100,200,300 are sorted as 1,10,100,2,20,200,3,30,300 Conventionally.
This programme sorts them in the correct numeric order i.e 1,2,3,10,20,30,100,200,300
2017-12-30 04:32:44 +08:00
|
|
|
|
2019-08-21 10:10:08 +08:00
|
|
|
vector<string> v(n);
|
|
|
|
cout << "Enter the string of Numbers\n";
|
|
|
|
for (int i = 0; i < n; i++)
|
|
|
|
{
|
|
|
|
cin >> v[i];
|
|
|
|
}
|
Added Numeric String Sort
Sorts an array of Numeric strings.
An array of strings like 1,2,3,10,20,30,100,200,300 are sorted as 1,10,100,2,20,200,3,30,300 Conventionally.
This programme sorts them in the correct numeric order i.e 1,2,3,10,20,30,100,200,300
2017-12-30 04:32:44 +08:00
|
|
|
|
2019-08-21 10:10:08 +08:00
|
|
|
sort(v.begin(), v.end());
|
|
|
|
cout << "Elements sorted normally \n";
|
|
|
|
for (int i = 0; i < n; i++)
|
|
|
|
{
|
|
|
|
cout << v[i] << " ";
|
|
|
|
}
|
|
|
|
cout << "\n";
|
Added Numeric String Sort
Sorts an array of Numeric strings.
An array of strings like 1,2,3,10,20,30,100,200,300 are sorted as 1,10,100,2,20,200,3,30,300 Conventionally.
This programme sorts them in the correct numeric order i.e 1,2,3,10,20,30,100,200,300
2017-12-30 04:32:44 +08:00
|
|
|
|
2019-08-21 10:10:08 +08:00
|
|
|
sort(v.begin(), v.end(), NumericSort);
|
|
|
|
cout << "Elements sorted Numerically \n";
|
|
|
|
for (int i = 0; i < n; i++)
|
|
|
|
{
|
|
|
|
cout << v[i] << " ";
|
|
|
|
}
|
Added Numeric String Sort
Sorts an array of Numeric strings.
An array of strings like 1,2,3,10,20,30,100,200,300 are sorted as 1,10,100,2,20,200,3,30,300 Conventionally.
This programme sorts them in the correct numeric order i.e 1,2,3,10,20,30,100,200,300
2017-12-30 04:32:44 +08:00
|
|
|
|
2019-08-21 10:10:08 +08:00
|
|
|
return 0;
|
Added Numeric String Sort
Sorts an array of Numeric strings.
An array of strings like 1,2,3,10,20,30,100,200,300 are sorted as 1,10,100,2,20,200,3,30,300 Conventionally.
This programme sorts them in the correct numeric order i.e 1,2,3,10,20,30,100,200,300
2017-12-30 04:32:44 +08:00
|
|
|
}
|