Sorting files in alphabetical order [duplicate] - flutter

This question already has answers here:
Is there a way to sort string lists by numbers inside of the strings?
(2 answers)
Closed 2 years ago.
I have a list of files like this :
['../page-1.png', '../page-3/png', '../page-2.png', '../page-6.png', ... '../page-9.png']
I need to sort these files according to the number at the end. I am using this :
imagePaths.sort((a, b) =>
a.toString().toLowerCase().compareTo(b.toString().toLowerCase()));
where imagePaths is the list of paths of those images.
THE ISSUE
This method works fine until there are only single digit file numbers, i.e. if there is a file called, '../page-10.png', it gets sorted into the list like so :
['../page-1.png', '../page-10.png', '../page-11.png', '../page-12.png', '../page-2.png', ..]
Now, how to sort them in their correct order?

I think I could give you an idea.
If the name pattern remains the same, then this gonna work out!
var pages = [
'../page-1.png',
'../page-2.png',
'../page-11.png',
'../page-12.png',
'../page-10.png',
];
pages.sort((a, b) =>
int.parse(a.split('-')[1].split('.')[0]).compareTo(int.parse(b.split('-')[1].split('.')[0])));
print(pages);
// [../page-1.png, ../page-2.png, ../page-10.png, ../page-11.png, ../page-12.png]
Just splitting the string and parsing the number and then sorting it.
Reference: https://api.dart.dev/stable/2.10.4/dart-core/Pattern-class.html
Hope that works!

A clean and safe solution will be by using a regex to extract the numbers instead of relying on the name format:
int parse(String page) {
return int.tryParse(page.split(RegExp(r'[^\d]')).singleWhere((value) => int.tryParse(value) != null));
}
list.sort((a, b) => parse(a) > parse(b) ? 1 : -1);

Whist there are technical solutions to this, as per the other answers, the fundamental problem is that the files are not being named correctly if the intention is to sort them by name at some point. The numeric portion of the name should be given sufficient leading zeroes to handle the maximum number of files eg. page-0001.png, page-0002.png, etc.

Related

DMN Nested Object Filering

Literal Expression = PurchaseOrders.pledgedDocuments[valuation.value=62500]
Purchase Order Structure
PurchaseOrders:
[
{
"productId": "PURCHASE_ORDER_FINANCING",
"pledgedDocuments" : [{"valuation" : {"value" : "62500"} }]
}
]
The literal Expression produces null result.
However if
PurchaseOrders.pledgedDocuments[valuation = null]
Return all results !
What am I doing wrong ?
I was able to solve using flatten function - but dont know how it worked :(
In your original question, it is not exactly clear to me what is your end-goal, so I will try to provide some references.
value filtering
First, your PurchaseOrders -> pledgedDocuments -> valuation -> value appears to be a string, so in your original question trying to filter by
QUOTE:
... [valuation.value=62500]
will not help you.
You'll need to filter to something more ~like: valuation.value="62500"
list projection
In your original question, you are projecting on the PurchaseOrders which is a list and accessing pledgedDocuments which again is a list !
So when you do:
QUOTE:
PurchaseOrders.pledgedDocuments (...)
You don't have a simple list; you have a list of lists, it is a list of all the lists of pledged documents.
final solution
I believe what you wanted is:
flatten(PurchaseOrders.pledgedDocuments)[valuation.value="62500"]
And let's do the exercise on paper about what is actually happening.
First,
Let's focus on PurchaseOrders.pledgedDocuments.
You supply PurchaseOrders which is a LIST of POs,
and you project on pledgedDocuments.
What is that intermediate results?
Referencing your original question input value for POs, it is:
[
[{"valuation" : {"value" : "62500"} }]
]
notice how it is a list of lists?
With the first part of the expression, PurchaseOrders.pledgedDocuments, you have asked: for each PO, give me the list of pledged documents.
By hypothesis, if you supplied 3 POs, and each having 2 documents, you would have obtained with PurchaseOrders.pledgedDocuments a resulting list of again 3 elements, each element actually being a list of 2 elements.
Now,
With flatten(PurchaseOrders.pledgedDocuments) you achieve:
[{"valuation" : {"value" : "62500"} }]
So at this point you have a list containing all documents, regardless of which PO.
Now,
With flatten(PurchaseOrders.pledgedDocuments)[valuation.value="62500"] the complete expression, you still achieve:
[{"valuation" : {"value" : "62500"} }]
Because you have asked on the flattened list, to keep only those elements containing a valuation.value equal to the "62500" string.
In other words iff you have used this expression, what you achieved is:
From any PO, return me the documents having the valuations' value
equals to the string 62500, regardless of the PO the document belongs to.

Deleting Specific Substrings in Strings [Swift] [duplicate]

This question already has answers here:
Any way to replace characters on Swift String?
(23 answers)
Closed 5 years ago.
I have a string var m = "I random don't like confusing random code." I want to delete all instances of the substring random within string m, returning string parsed with the deletions completed.
The end result would be: parsed = "I don't like confusing code."
How would I go about doing this in Swift 3.0+?
It is quite simple enough, there is one of many ways where you can replace the string "random" with empty string
let parsed = m.replacingOccurrences(of: "random", with: "")
Depend on how complex you want the replacement to be (remove/keep punctuation marks after random). If you want to remove random and optionally the space behind it:
var m = "I random don't like confusing random code."
m = m.replacingOccurrences(of: "random ?", with: "", options: [.caseInsensitive, .regularExpression])

Iterating through all but the last index of an array

I understand that in Swift 3 there have been some changes from typical C Style for-loops. I've been working around it, but it seems like I'm writing more code than before in many cases. Maybe someone can steer me in the right direction because this is what I want:
let names : [String] = ["Jim", "Jenny", "Earl"]
for var i = 0; i < names.count - 1; i+=1 {
NSLog("%# loves %#", names[i], names[i+1])
}
Pretty simple stuff. I like to be able to get the index I'm on, and I like the for-loop to not run if names.count == 0. All in one go.
But it seems like my options in Swift 3 aren't allowing me this. I would have to do something like:
let names : [String] = ["Jim", "Jenny", "Earl"]
if names.count > 0 {
for i in 0...(names.count - 1) {
NSLog("%# loves %#", names[i], names[i+1])
}
}
The if statement at the start is needed because my program will crash in the situation where it reads: for i in 0...0 { }
I also like the idea of being able to just iterate through everything without explicitly setting the index:
// Pseudocode
for name in names.exceptLastOne {
NSLog("%# loves %#", name, name.plus(1))
}
I feel like there is some sort of syntax that mixes all my wants, but I haven't come across it yet. Does anyone know of a way? Or at least a way to make my code more compact?
UPDATE: Someone suggested that this question has already been asked, citing a SO post where the solution was to use something to the degree of:
for (index, name) in names.enumerated {}
The problem with this when compared to Hamish's answer is that I only am given the index of the current name. That doesn't allow me to get the value at index without needing to do something like:
names[index + 1]
That's just one extra variable to keep track of. I prefer Hamish's which is:
for i in names.indices.dropLast() {
print("\(names[i]) loves \(names[i + 1])")
}
Short, simple, and only have to keep track of names and i, rather than names, index, and name.
One option would be to use dropLast() on the array's indices, allowing you to iterate over a CountableRange of all but the last index of the array.
let names = ["Jim", "Jenny", "Earl"]
for i in names.indices.dropLast() {
print("\(names[i]) loves \(names[i + 1])")
}
If the array has less than two elements, the loop won't be entered.
Another option would be to zip the array with the array where the first element has been dropped, allowing you to iterate through the pairs of elements with their successor elements:
for (nameA, nameB) in zip(names, names.dropFirst()) {
print("\(nameA) loves \(nameB)")
}
This takes advantage of the fact that zip truncates the longer of the two sequences if they aren't of equal length. Therefore if the array has less than two elements, again, the loop won't be entered.

How do I sort a list of Doubles in Scala? [duplicate]

This question already has answers here:
How do I sort an array in Scala?
(7 answers)
Closed 8 years ago.
How do I sort a simple list of Doubles in Scala?
var dubs = List(1.3,4.5,2.3,3.2)
I think my question may not have accurately reflected my specific problem, since I realize now that dubs.sorted will work just fine for the above. My problem is as follows, I have a string of doubles "2.3 32.4 54.2 1.33" that I'm parsing and adding to a list
var numsAsStrings = l.split("\\s");
var x = List(Double);
var i = 0;
for( i <- 0 until numsAsStrings.length) {
x :+ numsAsStrings(i).toDouble;
}
So, I would think that I could just call x.sorted on the above, but that doesn't work... I've been looking over the sortBy, sorted, and sortWith documentation and various posts, but I thought the solution should be simpler. I think I'm missing something basic, regardless.
Use the sorted method
dubs.sorted // List(1.3, 2.3, 3.2, 4.5)

Cypher: Problems with comparison

I'm trying to excecute a cypher query for a no4j database on gwt.
I stored in some nodes int values as property detail. If I'm using neoclipe right, I noticed now, that this values are stored in the database as String values.
In my query I have the following part which does not work:
START ...
MATCH node-[:SomeTag]->intnode
WHERE intnode.detail < 10
RETURN ...
and I get:
Don't know how to compare that. Left: 15; Right: 10: Don't know how to compare that: Left: 15; Right: 10
So intnode.detail < 10 does not work. I also tried this: intnode.detail < STR(10), because I thought it will compare the hash values or ascii values, but I got the same error.
EDIT:
I read, that it is possible to set the #GraphProperty while storing data, but how can I do that in gwt?
I mean if I have a node and I could e.g. write
Object obj = (Object) 10;
node.setProperty("detail", obj);
How can I now tell neo4j, that obj is an int?
This answer is mostly focused on your initial question - not on the question you´ve added in the EDIT-part.
I just had a similar problem with a comparison inside the WHERE-part of a cypher query. I tried to do something like
MATCH ...
WHERE value > 1
which caused an error message very similar to yours. After some testing I´ve found out that the query works, if I add single quotes. This is my solution:
MATCH ...
WHERE value > '1'
(note the quotes)
Ive also noticed, that this doesnt work with double quotes
I hope this helps you and/or anyone else who encounters this problem :)
I think the intnode.detail value in stored as string , so you wont able to compare with integer value.
You have to do like this
START ...
MATCH node-[:SomeTag]->intnode
WHERE intnode.detail < "10"
RETURN ...