gpt4 book ai didi

java - 如何按名字字母顺序和姓氏排序

转载 作者:行者123 更新时间:2023-11-30 06:29:05 25 4
gpt4 key购买 nike

我有 3 个元素,它们有名字和姓氏,它是一个按钮。我需要检查它们是否按名字排序,然后按姓氏按字母顺序排序。元素的页面对象是 ListV_Sorted。我想我只是按名字元素排序。 0 元素 = Jack Daniels,1 元素 = John Fera,2 元素 = Mike Durov。 ListV 是元素的页面对象。我收到一个错误,它说预期为 true,但发现为 false。

@SuppressWarnings("unchecked")
public void sortingOrder() {

Log.log(driver).info("About to Alphebetically Sort");
List<MobileElement> products_MobileElements = new LinkedList<MobileElement>();
products_MobileElements = (List<MobileElement>) TicketPassesNames;
LinkedList<String> product_names = new LinkedList<String>();

for(int i=0;i<products_MobileElements.size();i++){

String s = products_MobileElements.get(i).getAttribute("checked");
String[] tokens = s.split("");
String firstName = "";
String lastName = "";
if(tokens.length > 0) {
firstName = tokens[0];
lastName = tokens[tokens.length -1];
product_names.add(s);
product_names.add(firstName);
product_names.add(lastName);
}
}
boolean result = checkAlphabeticalOrder(product_names);

Assert.assertEquals(checkAlphabeticalOrder(product_names), true);
Log.log(driver).info("Tickest Passes names are in alphabetical order.");
System.out.println(result);
}

//Method takes a String to Sort AlphabeticalLy
public static boolean checkAlphabeticalOrder(LinkedList<String> product_names){

String previous = ""; // empty string

for (final String current: product_names) {
if (current.compareTo(previous) < 0)
return false;
previous = current;
}

return true;
}

最佳答案

您应该使用java.util.TreeSet而不是java.util.LinkedList来使用数据结构对输入进行排序。

我创建了一个稍微修改过的代码版本,它在程序执行后打印“true”。但主要区别在于 java.util.TreeSetCollection 接口(interface)的使用。

这是更改后的版本:

import java.text.ParseException;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.TreeSet;

public class SimpleTests2 {

public static void main(String[] args) throws ParseException {

sortingOrder();
}

//Method takes a String to Sort AlphabeticalLy
public static boolean checkAlphabeticalOrder(Collection<String> product_names) {

String previous = ""; // empty string

for (final String current : product_names) {
if (current.compareTo(previous) < 0)
return false;
previous = current;
}

return true;
}

@SuppressWarnings("unchecked")
public static void sortingOrder() {

// Log.log(driver).info("About to Alphebetically Sort");
List<MobileElement> products_MobileElements;
products_MobileElements = Arrays.asList(
new MobileElement("Mike Durov"), new MobileElement("Jack Daniels"), new MobileElement("John Fera"));
// Here is the main change to your code!
Collection<String> product_names = new TreeSet<>();

for (int i = 0; i < products_MobileElements.size(); i++) {

String s = products_MobileElements.get(i).getAttribute("checked");
String[] tokens = s.split(" ");
String firstName = "";
String lastName = "";
if (tokens.length > 0) {
firstName = tokens[0];
lastName = tokens[tokens.length - 1];
product_names.add(s);
product_names.add(firstName);
product_names.add(lastName);
}
}
boolean result = checkAlphabeticalOrder(product_names);

// Assert.assertEquals(checkAlphabeticalOrder(product_names), true);
// Log.log(driver).info("Tickest Passes names are in alphabetical order.");
System.out.println(result);
System.out.println(product_names);
}


private static class MobileElement {

private String name;

public MobileElement(String name) {
this.name = name;
}

public String getAttribute(String checked) {
return name;
}
}
}

关于java - 如何按名字字母顺序和姓氏排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46492364/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com