-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortarraybystringlength.cpp
More file actions
36 lines (28 loc) · 980 Bytes
/
Sortarraybystringlength.cpp
File metadata and controls
36 lines (28 loc) · 980 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include<iostream>
#include<algorithm>
#include<vector>
#include <array>
#include<cmath>
#include <string>
#include <iostream>
#include <string>
using namespace std;
vector<string> sortByLength(vector<string> array) {
sort(array.begin(), array.end(), [](const string& a, const string& b) {
return a.length() < b.length();
});
return array;
}
int main()
{
sortByLength({ "Beg", "Life", "I", "To" });
return 0;
}
/*Description:
Write a function that takes an array of strings as an argument and returns a sorted array containing the same strings, ordered from shortest to longest.
For example, if this array were passed as an argument:
["Telescopes", "Glasses", "Eyes", "Monocles"]
Your function would return the following array:
["Eyes", "Glasses", "Monocles", "Telescopes"]
All of the strings in the array passed to your function will be different lengths, so you will not have to decide how to order multiple strings of the same length.
*/