N1QL follows Collation defined here
In your case all are strings. With in strings ASCII character set space , numbers, letters. There is no easy way to get expected results.
INSERT INTO default VALUES (UUID(),{"f1":"1"}), (UUID(),{"f1":"foo"}), (UUID(),{"f1":"11"}), (UUID(),{"f1":"bar"}), (UUID(),{"f1":"1foo"}), (UUID(),{"f1":"3"}), (UUID(),{"f1":"1 foo"}), (UUID(),{"f1":"2 bar"});
SELECT RAW f1
FROM default
WHERE f1 IS NOT NULL
ORDER BY IFMISSINGORNULL(TO_NUMBER(f1),"") ASC, CONTAINS(f1," ") ASC, f1 ASC;
"results": [
"1",
"3",
"11",
"1foo",
"bar",
"foo",
"1 foo",
"2 bar"
]
Natural order of string will be “foo11”, “foo2” in string comparison you compare character by character .
What you are looking is separate string into 2 parts string part, numeric part(convert to number ) and sort on that. Unless you have clear separator it is not possible in N1QL. You can do in application.