Sort by

recency

|

956 Discussions

|

  • + 0 comments

    include

    include

    include

    using namespace std; bool compare(const string &a, const string &b) { if (a.length() != b.length()) return a.length() < b.length(); return a < b; }

    vector bigSorting(vector unsorted) { sort(unsorted.begin(), unsorted.end(), compare); return unsorted; }

    int main() { int n; cin >> n; vector unsorted(n);

    for (int i = 0; i < n; ++i) {
        cin >> unsorted[i];
    }
    
    vector<string> sorted = bigSorting(unsorted);
    
    for (const string &num : sorted) {
        cout << num << "\n";
    }
    
    return 0;
    

    }

  • + 0 comments

    include

    include

    using namespace std;

    int main() { int n; cin >> n;

    const int MAX_LEN = 100;  
    char arr[100][MAX_LEN];  
    
    
    for (int i = 0; i < n; ++i) {
        cin >> arr[i];
    }
    
    
    for (int i = 0; i < n - 1; ++i) {
        for (int j = 0; j < n - i - 1; ++j) {
            int len1 = strlen(arr[j]);
            int len2 = strlen(arr[j + 1]);
            if (len1 > len2 || (len1 == len2 && strcmp(arr[j], arr[j + 1]) > 0)) {
                char temp[MAX_LEN];
                strcpy(temp, arr[j]);
                strcpy(arr[j], arr[j + 1]);
                strcpy(arr[j + 1], temp);
            }
        }
    }
    
    
    for (int i = 0; i < n; ++i) {
        cout << arr[i] << endl;
    }
    
    return 0;
    

    }

  • + 0 comments

    include

    include

    using namespace std;

    int main() { int n; cin >> n;

    const int MAX_LEN = 100;  
    char arr[100][MAX_LEN];  
    
    
    for (int i = 0; i < n; ++i) {
        cin >> arr[i];
    }
    
    
    for (int i = 0; i < n - 1; ++i) {
        for (int j = 0; j < n - i - 1; ++j) {
            int len1 = strlen(arr[j]);
            int len2 = strlen(arr[j + 1]);
            if (len1 > len2 || (len1 == len2 && strcmp(arr[j], arr[j + 1]) > 0)) {
                char temp[MAX_LEN];
                strcpy(temp, arr[j]);
                strcpy(arr[j], arr[j + 1]);
                strcpy(arr[j + 1], temp);
            }
        }
    }
    
    
    for (int i = 0; i < n; ++i) {
        cout << arr[i] << endl;
    }
    
    return 0;
    

    }

  • + 0 comments
    def bigSorting(unsorted):
        # Write your code here
        return sorted(unsorted, key=lambda x: (len(x), x))
    
  • + 0 comments
    public static List<String> bigSorting(List<String> unsorted) {
        Map<Integer, List<String>> map = unsorted.stream().collect(Collectors.groupingBy(String::length));
        List<String> result = new ArrayList<>();
        map.keySet().stream().sorted().forEach(key -> {
            List<String> r = map.get(key);
            r.sort((s1, s2) -> s1.compareTo(s2));
            result.addAll(r);
        });
        return result;
    }