Solution to Ex. 07.
authorLukas Jiriste <ljiriste@student.42prague.com>
Wed, 28 Jun 2023 08:22:50 +0000 (10:22 +0200)
committerLukas Jiriste <ljiriste@student.42prague.com>
Wed, 28 Jun 2023 08:22:50 +0000 (10:22 +0200)
ex07/ft_advanced_sort_string_tab.c [new file with mode: 0644]

diff --git a/ex07/ft_advanced_sort_string_tab.c b/ex07/ft_advanced_sort_string_tab.c
new file mode 100644 (file)
index 0000000..c2a7779
--- /dev/null
@@ -0,0 +1,43 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_advanced_sort_string_tab.c                      :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2023/06/28 07:13:52 by ljiriste          #+#    #+#             */
+/*   Updated: 2023/06/28 10:21:06 by ljiriste         ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+void   ft_swap(char **s1, char **s2)
+{
+       char    *temp;
+
+       temp = *s1;
+       *s1 = *s2;
+       *s2 = temp;
+       return ;
+}
+
+void   ft_advanced_sort_string_tab(char **tab, int (*cmp)(char *, char *))
+{
+       int     i;
+       int     j;
+
+       if (!*tab)
+               return ;
+       i = 0;
+       while (tab[i + 1])
+       {
+               j = i + 1;
+               while (tab[j])
+               {
+                       if (cmp(tab[i], tab[j]) > 0)
+                               ft_swap(tab + i, tab + j);
+                       ++j;
+               }
+               ++i;
+       }
+       return ;
+}