Reputation: 17
Hi I have a groovy code for JIRA. Here I need to sort or remove duplicate value from the list.
Ex :
["154515 Sawgrass",
"170985 Mexico APIs for Payments",
"153026 CitiCards Consumer and Business Account Online (authenticated pgs), No CSI App ID",
"153890 GC Citibank Online - Singapore IPB v3",
"144564 Citibank Online (CBOL) US, Zack Dummy CSI #3",
"171706 Quip",
"167518 GC Eclipse [Teller]",
"167518 GC Eclipse [Signature]]",]
the above is the result, from where I need a sorting like ... 167518 - with this number there are two values, but I need to store only one 167518, I dont want to store multi value. or else in simple word I need to sort the list in this way that if number is same but with multiple values, I need to store only 1 value with same number.
// From here Child Issue Details Starts....
def issue = event.issue as Issue
def issueManager = ComponentAccessor.getIssueManager() ;
def customField = ComponentAccessor.getComponent(CustomFieldManager).getCustomFieldObject("customfield_10602")
log.debug("Printing Custom Field : --" + customField)
def impactedAppValues = customField.getValueFromIssue(issue) as String
log.info "Printing Custom Field with Issuee Value : --" + impactedAppValues
String[] elements = impactedAppValues.split("\\s*[;]\\s*");
log.info "Printing elements with Issuee Value : --" + elements
List<String> fixedLenghtList = Arrays.asList(elements);
ArrayList<String> listOfString = new ArrayList<String>(fixedLenghtList);
log.info "Printing listOfString with Issuee Value : --" + listOfString
// From here parent Issue Details Starts....
def cf = ComponentAccessor.customFieldManager.getCustomFieldObjectByName("Parent Link")
def parentValue = issue.getCustomFieldValue(cf)
log.info "Printing parentValue value :-----" + parentValue
def planviewProjectSide = ComponentAccessor.getComponent(CustomFieldManager).getCustomFieldObject("customfield_10602");
Issue PlanviewIssue = issueManager.getIssueObject("" + parentValue);
def parentCustomField = planviewProjectSide.getValueFromIssue(PlanviewIssue) as String
log.info "Printing parentCustomField value :-----" + parentCustomField
String[] elements1 = parentCustomField.split("\\s*[;]\\s*");
log.info "Printing elements for Parent Issuee Value : --" + elements1
List<String> fixedLenghtList1 = Arrays.asList(elements1);
ArrayList<String> listOfString1 = new ArrayList<String>(fixedLenghtList1);
log.info "Printing listOfString1 For Parent Issuee Value : --" + listOfString1
listOfString1.addAll(listOfString)
log.info "Printing listOfString1 Againnnnnnnnn For Combined Value of 1 & 2--" + listOfString1
listOfString1.unique()
log.info "Printing listOfString1 Unique Values of 1 & 2--" + listOfString1
parentCustomField = listOfString1 as String
log.info "Printing parentCustomField -----" + parentCustomField
here I have gave the code for what I have written till now.
Is this possible to to sort like the above? I mean we can remove the duplicates but how to removes the number from a value or String?
Upvotes: 1
Views: 1118
Reputation: 73
Original list:
def list = ["154515 Sawgrass",
"170985 Mexico APIs for Payments",
"153026 CitiCards Consumer and Business Account Online (authenticated pgs) No CSI App ID",
"153890 GC Citibank Online - Singapore IPB v3",
"144564 Citibank Online (CBOL) US, Zack Dummy CSI #3",
"171706 Quip",
"167518 GC Eclipse [Teller]",
"167518 GC Eclipse [Signature]"]
First, split each element and take the numbers part:
def numbersList = []
list?.each {
numbersList.add(it?.split(" ")[0])
}
Apply the unique property:
def uniqueNumbers = numbersList.unique()
Then, for each unique value, get the data from the original list:
def result = []
uniqueNumbers?.each { number ->
result.add(list.find { it.contains(number?.toString()) })
}
Sort the final results:
result.sort(true)
And we get:
144564 Citibank Online (CBOL) US, Zack Dummy CSI #3
153026 CitiCards Consumer and Business Account Online (authenticated pgs) No CSI App ID
153890 GC Citibank Online - Singapore IPB v3
154515 Sawgrass
167518 GC Eclipse [Teller]
170985 Mexico APIs for Payments
171706 Quip
Upvotes: 0
Reputation: 20699
Sounds like you want to use groupBy
method:
def list = ['154515 Sawgrass', '170985 Mexico APIs for Payments', '153026 CitiCards Consumer and Business Account Online (authenticated pgs)', 'No CSI App ID', '153890 GC Citibank Online - Singapore IPB v3', '144564 Citibank Online (CBOL) US', 'Zack Dummy CSI #3', '171706 Quip', '167518 GC Eclipse [Teller]', '167518 GC Eclipse [Signature]']
List result = list.groupBy{ String s ->
String num
s.eachMatch( /^(\d+).+$/ ){ num = it[ 1 ] }
num
}.findResults{ String num, List<String> vals ->
num ? vals.sort().first() : null
}
1st the groupBy is called groupping by "id" using the regex, as a result you are getting a map keyed by the "id" and valued with a list of matching strings.
Then the findResults is called to filter out mismatching values like 'No CSI App ID'
, and sort the match lists alphabetically and get the first()
(or last()
) element out of it.
The result would look like:
[154515 Sawgrass, 170985 Mexico APIs for Payments, 153026 CitiCards Consumer and Business Account Online (authenticated pgs), 153890 GC Citibank Online - Singapore IPB v3, 144564 Citibank Online (CBOL) US, 171706 Quip, 167518 GC Eclipse [Signature]]
Upvotes: 0
Reputation: 37008
You could extract the ID and put it in a SortedMap. e.g.
def data =
[ "154515 Sawgrass"
, "171706 Quip"
, "167518 GC Eclipse [Teller]"
, "167518 GC Eclipse [Signature]]" ]
println(
data.collectEntries(new TreeMap()) {
[it.findAll(/\d+/).first().toLong(), it]
}
)
// → [154515:154515 Sawgrass, 167518:167518 GC Eclipse [Signature]], 171706:171706 Quip]
Upvotes: 1
Reputation: 24468
Assume we start with a list like this:
def list = ["154515 Sawgrass",
"170985 Mexico APIs for Payments",
"153026 CitiCards Consumer and Business Account Online (authenticated pgs) No CSI App ID",
"153890 GC Citibank Online - Singapore IPB v3",
"144564 Citibank Online (CBOL) US, Zack Dummy CSI #3",
"171706 Quip",
"167518 GC Eclipse [Teller]",
"167518 GC Eclipse [Signature]"]
Then we can start with a new list and a set of values:
def listUniqueValues = []
def valuesAlreadySeen = new HashSet()
Then we iterate over the list, checking to see if the current value is in the set of "previously seen" values:
list.each { item ->
def value = item.split(" ")[0]
if (! valuesAlreadySeen.contains(value)) {
listUniqueValues << item
}
valuesAlreadySeen << value
}
then we can sort listUniqueValues
and print:
listUniqueValues.sort() { a,b -> a <=> b }.each { println it }
to get
144564 Citibank Online (CBOL) US, Zack Dummy CSI #3
153026 CitiCards Consumer and Business Account Online (authenticated pgs) No CSI App ID
153890 GC Citibank Online - Singapore IPB v3
154515 Sawgrass
167518 GC Eclipse [Teller]
170985 Mexico APIs for Payments
171706 Quip
NOTE: this isn't the most efficient method, nor the "Grooviest" (there is a cooler way with the inject
method). But it is straight-forward and hopefully easy to understand.
Upvotes: 0
Reputation: 1227
If I understand your issue right, you need to leave only one item with the same number( the number is the first word (let's call it leading number))? Firstly you can sort your list. In result strings with the same "leading number" will be placed in adjacent cells. Then you can iterate through the list, extract "leading number" (e.g. by regexp) from i-1 and i elements, and, if they are the same, just remove element i from the list.
Upvotes: 1