我在 Jmeter 中有一个 API 中按名称排序的场景。
按名称获取名称顺序的 API 响应返回如下响应,
{“值”:[{“ID”:“efe36a3b-8e7a-4850-a580-b9821195daca”,“名称”:“Harry”},{“ID”:“efe36a3b-9e7a-4850-a580-b9821195dacb”, "name":"Jerry"},{"ID":"efe36a3b-8f7a-4850-a580-b9821195dacc","name":"Kate"},{"ID":"efe36a3b-8g7a-4850-a580-b9821195dacd ","name":"Louis"}],"next":"https://localhost:8080/v1/empNames?$skip=20&$orderby=id desc"}
现在我想验证名称和 ID 是否按升序排列。
上述场景的代码片段如下,
ID 是使用 json 提取器提取的
id1: efe36a3b-8e7a-4850-a580-b9821195daca
id2: ffe36a3b-9e7a-4850-a580-b9821195dacb
String id1 = vars.get("id1");
String id2 = vars.get("id2");
if ( id1 > id2){
log.info("id1 is greater and its value is + " + id1);
log.info("id2 is lesser and its value is + " + id2);
SampleResult.setSuccessful(true);// this will set the status of the sampler to pass
}
if ( id1 < id2){
log.info("id2 is greater and its value is + " + id2);
SampleResult.setSuccessful(false); // this will set the status of the sampler to failure
}
看来你在找String.compareTo()函数
按字典顺序比较两个字符串。比较基于字符串中每个字符的 Unicode 值。此 String 对象表示的字符序列按字典顺序与参数字符串表示的字符序列进行比较。如果此 String 对象在字典顺序上位于参数字符串之前,则结果为负整数。如果此 String 对象按字典顺序跟随参数字符串,则结果为正整数。如果字符串相等,则结果为零;否则compareTo 恰好在 equals(Object) 方法返回 true 时返回 0。
更多信息: