Selenium Java:Collection中的数字从低到高排序

时间:2016-09-04 19:23:46

标签: java sorting selenium selenium-webdriver

    driver.get("www.exmpale.com");
    Thread.sleep(7000);
    driver.findElement(By.cssSelector(".side-tab.side-generator")).click();
    driver.manage().window().maximize();
    Thread.sleep(30000);
    // to find the 5 numbers in list1 and list 2 present in the header and
    // add them in the list
    for (int i = 0; i < 7; i++) {

        numberList1
                .add(
                        driver
                        .findElements(By.cssSelector(".generated-line"))
                        .get(0)
                        .findElements(
                                By.cssSelector(".generated-ball.animated.bounceIn>div")                     
                                )

                        .get(i).getText()
                        );
driver.switchTo().frame(driver.findElement(By.id("lotto-frame-games")));

// to find the numbers in the 2 boxes and add them in comparelist1 and
// comparelist2
for (int i = 0; i < 7; i++) {

    compareList1
            .add(driver
                    .findElements(
                            By.cssSelector(".select_num_col_part"))
                    .get(0)
                    .findElements(By.cssSelector(".main_active, .extra_active"))
                    .get(i).getText());


//to sort the lists so that lists can be compared easily.
Collections.sort(numberList1);
Collections.sort(compareList1);
System.out.println(numberList1.toString());
System.out.println(compareList1.toString());

结果:

 [13, 16, 27, 34, 5, 7, 7]
 [13, 16, 27, 34, 5, 7, 7]

我希望排序从低到高,我该怎么做?

1 个答案:

答案 0 :(得分:0)

您已拨打Collections.sort(numberList1);,因此需要对List进行排序。

但是,您的numberList1包含String。如果您使用泛型,则可以在创建列表的位置立即看到。它将是这样的List<String> numberList1 = new ...

因此,排序将为lexicographic,就像您要对文本进行排序一样。这就是123之前出现的原因,1的第一个字符12小于3,依此类推。

您只需将列表更改为整数列表:List<Integer>。然后你的排序将按你的意愿。您可以使用StringIntegerInteger.parseInt(text)转换为new Integer(text)

干杯