0

I have one string array i.e.

["Anesthesiology", "Ayurveda", "Cardiology", "Cardiothoracic Surgery", "Clinical Microbiology and Infectious Diseases", "Cosmetology", "Critical Care Medicine", "Dental Surgery", "Dermatology", "Electrohomoepathy", "Emergency Medicine", "Endocrinology", "ENT", "Gastroenterology", "Gastrointestinal Surgery", "General Medicine", "General Surgery", "Genetics", "Gynaecology", "Gynaecology and Obstetrics", "Hematology", "Homeopathy", "Immunology", "Internal Medicine and Diabetology", "Nephrology", "Neurology", "Neurosurgery", "Nutrition and Dietetics", "Oncology", "Ophthalmology", "Orthopaedics", "Paediatrics", "Pain Management Specialist", "Pathology", "Pharmacology", "Physical Medicine", "Physiotherapy", "Plastic Surgery", "Podiatry", "Psychiatry", "Psychology", "Pulmonology", "Radiation Oncology", "Radiology", "Rheumatology", "Sexologist", "Urology", "Vascular Surgery", "Others"]

I am using search bar to search by characters in this array. I am using this code to search using search text:

for i in 0..<self.completeArray.count{

 if String(self.completeArray[i].lowercased() ).contains("\(self.searchText!.lowercased())"){
                                outputArray.append(self.completeArray[i])
                            }
}

Now suppose If I am searching by character 'P' then my output is coming this:

["Electrohomoepathy", "Homeopathy", "Nephrology", "Ophthalmology", "Orthopaedics", "Paediatrics", "Pain Management Specialist", "Pathology", "Pharmacology", "Physical Medicine", "Physiotherapy", "Plastic Surgery", "Podiatry", "Psychiatry", "Psychology", "Pulmonology"]

But the expected output I want like:

["Paediatrics", "Pain Management Specialist", "Pathology", "Pharmacology", "Physical Medicine", "Physiotherapy", "Plastic Surgery", "Podiatry", "Psychiatry", "Psychology", "Pulmonology", "Electrohomoepathy", "Homeopathy", "Nephrology", "Ophthalmology", "Orthopaedics"]

So basically output array should show first all the element starts from the searchtext character.

Can anyone please help me on this? Thanks in Advance!

Anand Gautam
  • 2,541
  • 3
  • 34
  • 70
  • [https://stackoverflow.com/questions/32664543/swift-startswith-method](https://stackoverflow.com/questions/32664543/swift-startswith-method) – HunterLion Mar 30 '22 at 12:36
  • And afterwards? If the "p" is in last position, do you expect it to be at the end of the array? – Larme Mar 30 '22 at 12:55
  • [How to sort an array of string by similarity to specific key](https://stackoverflow.com/a/47132167/2303865) – Leo Dabus Mar 30 '22 at 13:02
  • 1
    If that's the case you can use `let filtered = initialArray.filter { $0.caseInsensitiveRange(of: searchText) != nil }; let sorted = filtered.sorted(by: { $0.caseInsensitiveRange(of: searchText)!.lowerBound < $1.caseInsensitiveRange(of: searchText)!.lowerBound })` where `extension String { func caseInsensitiveRange(of searchText: String) -> Range? { range(of: searchText, options: .caseInsensitive, locale: .current) } }` – Larme Mar 30 '22 at 13:02
  • @Larme check the duplicated link. Feel free to post your approach there as well – Leo Dabus Mar 30 '22 at 13:02
  • @Larme - can you add `[.caseInsensitive, .anchored]`, – Anbu.Karthik Mar 30 '22 at 13:04
  • 2
    @Anbu.Karthik No, since I don't search only for prefix, as `Ophthalmology` would have been removed. I didn't check also the default alphabectical since the initial array is sorted. – Larme Mar 30 '22 at 13:06
  • Thank you for your response. I'm checking now @Larme – Anand Gautam Mar 30 '22 at 13:08
  • This is what I was looking for. Thank you all for your early response and help:) – Anand Gautam Mar 30 '22 at 13:10

0 Answers0