-4

suppose I have different sets of strings like:

String 1 :- Indian Chess League 2021 Online (Play From Home) E Sports price 99 onwards

String 2 :- Weekly Chess Maha Muqabla Online (Play From Home) E Sports price 225 onwards

String 3 :- Call of Duty Mobile Tournament Online (Play From Home) E Sports price 300 onwards

String 4 :- Green 10KM Challenge – Get India`s Biggest Medal Your Place and Your Time: India E Sports price 399

String 5 :- Free Fire Solo And Squad (Bermuda) Online (Play From Home) E Sports price 100 onwards

*String 6 :- Johnson Call Of Duty Mobile Tournament Online (Play From Home) E Sports price 100 onwards

String 8 :- Online Martial Arts Coaching for Kids at Home Online Streaming Mixed Martial Arts price 1180 onwards

String 9:- Call of Duty Battle Royale In Aid Of Sightsavers Online (Play From Home): India E Sports price 100

Here, I have print the strings in the ascending order based on the price, so how can I do it.

Thanks in advance. :) Preferred language is JAVA

Shivam Roy
  • 1,961
  • 3
  • 10
  • 23
  • If the preferred language is java then why did you put the `python` tag? – rdas May 12 '21 at 17:48
  • 1
    First define the [_natural ordering_](https://docs.oracle.com/javase/tutorial/collections/interfaces/order.html) for such prices and then implement `Comparable` accordingly. – trashgod May 12 '21 at 17:49

2 Answers2

1

You can do the following in Java:

    strings.stream()
            .sorted(Comparator.comparingDouble(s -> {
                int priceIndex = s.indexOf("price") + "price ".length();
                int spaceAfterPrice = s.indexOf(" ", priceIndex);
                int endPriceIndex = spaceAfterPrice != -1 ? spaceAfterPrice:s.length();
                return Double.parseDouble(s.substring(priceIndex, endPriceIndex));
            }))
            .forEach(System.out::println);

Output:

Indian Chess League 2021 Online (Play From Home) E Sports price 99 onwards
Free Fire Solo And Squad (Bermuda) Online (Play From Home) E Sports price 100 onwards
Johnson Call Of Duty Mobile Tournament Online (Play From Home) E Sports price 100 onwards
Call of Duty Battle Royale In Aid Of Sightsavers Online(Play From Home):India E Sports price 100
Weekly Chess Maha Muqabla Online (Play From Home) E Sports price 225 onwards
Call of Duty Mobile Tournament Online (Play From Home) E Sports price 300 onwards
Green 10KM Challenge – Get India`s Biggest Medal Your Place and Your Time: India E Sports price 399
Online Martial Arts Coaching for Kids at Home Online Streaming Mixed Martial Arts price 1180 onwards
Most Noble Rabbit
  • 2,728
  • 2
  • 6
  • 12
  • Note now this approach relies on the _natural ordering_ of [`Double`](https://docs.oracle.com/javase/8/docs/api/java/lang/Double.html). – trashgod May 12 '21 at 23:43
0

You can probably use a comparator and remove all of the non-number characters then compare those numbers and order them accordingly. maybe this can point you in the correct direction: Sorting Strings that contains number in Java

Maez
  • 1